Hi partner,
If you want to use C# to do this, you could refer to my code here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace plugin2._0
{
public class Birthday : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
var birthday = entity["new_birthday1"].ToString();
var dateBirthday = Convert.ToDateTime(birthday);
var now = DateTime.Now;
int age = now.Year - dateBirthday.Year;
entity["new_age"] = age;
service.Update(entity);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(ex.InnerException.ToString(), ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
You could register this plugin in pre-operation or post-operation update the record.
For more description about plug-in, please refer to the following links.
https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/entity-operations-update-delete
https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/org-service/samples/basic-followup-plugin
Hope it helps.
Best Regards,
Leo