RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value
HI Gowtham,
If you can share the error then i will try to help you.
And create plugin in below way:
Logic:
1. Plugin should be register on Create/Update/Delete of "Service Product" entity.
2. Plugin will perform the below logic to refresh the "Total Actual Amount" on opportunity:
Plugin Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
namespace Training.Plugins
{
public class RefreshEntitlementRollupFields : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory iOrganizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationService service = iOrganizationServiceFactory.CreateOrganizationService(context.UserId);
tracing.Trace("Inside Execute function");
switch (context.MessageName)
{
case "Create":
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] != null)
{
RefreshRollupFields(tracing, service, (Entity)context.InputParameters["Target"]);
}
break;
case "Update":
if (context.PostEntityImages.Contains("postImage") &&
context.PostEntityImages["postImage"] != null)
{
RefreshRollupFields(tracing, service, (Entity)context.PostEntityImages["postImage"]);
}
break;
case "Delete":
if (context.PreEntityImages.Contains("preImage") &&
context.PreEntityImages["preImage"] != null)
{
RefreshRollupFields(tracing, service, (Entity)context.PreEntityImages["preImage"]);
}
break;
}
tracing.Trace("Execute function ended");
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
private void RefreshRollupFields(ITracingService tracing, IOrganizationService service, Entity serviceProductEnt)
{
tracing.Trace("Inside RefreshRollupFields");
//add logical name of
string[] fieldList = { "" };
if (serviceProductEnt != null && serviceProductEnt.Attributes.Contains(""))
{
EntityReference opportunityObj = serviceProductEnt.GetAttributeValue("");
if (opportunityObj != null)
{
tracing.Trace("Opportunity lookup found and ID is " opportunityObj.Id);
for (int i = 0; i < fieldList.Length; i )
{
tracing.Trace("Calculating for " fieldList[i]);
string EntityLogicalName = opportunityObj.LogicalName;
Guid EntityID = opportunityObj.Id;
var calcularRollup = new CalculateRollupFieldRequest
{
Target = new EntityReference(EntityLogicalName, EntityID),
FieldName = fieldList[i]
};
var calcularRollupResult = (CalculateRollupFieldResponse)service.Execute(calcularRollup);
}
tracing.Trace("Fields updated");
}
}
tracing.Trace("RefreshRollupFields ended");
}
}
}
In above code please remove the place holder values enclosed in <> this.
Thanks,
Pradeep.
Please mark this as VERIFIED if it helps.