Hi,
I am trying to write a plugin where everytime a child record gets created.updated/deleted, the associated parent record should be updated with sum of all child record's amount value.
Here is my plugin(below) which works fine updating the parent record with sum of all the existing child record's amount value except the newly created(about to be) child record's amount value.
Also, child record won't get created and I just get a business process error(no useful information of error).
So, updates the parent record but fails to create child record. What am I doing wrong here? Can anybody please suggest? Thanks for any help!
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // Get a reference to the Organization service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); if (context.InputParameters != null) { Entity entity = (Entity)context.InputParameters["Target"]; //get the associated parent id EntityReference a = (EntityReference)entity.Attributes["new_xxx"]; decimal totalAmount = 0; try { //fetchxml to get the sum total of expense amount string estimatedvalue_sum = string.Format(@" ", a.Id); EntityCollection estimatedvalue_sum_result = service.RetrieveMultiple(new FetchExpression(estimatedvalue_sum)); foreach (var c in estimatedvalue_sum_result.Entities) { totalAmount = ((Money)((AliasedValue)c["amount_sum"]).Value).Value; } //updating the field on the aaa Entity aaa = new Entity("new_xxx"); aaa.Id = a.Id; aaa.Attributes.Add("new_amount", new Money(totalAmount)); service.Update(aaa);