Hi guys,
I have the following situation:
An opportunity can have multiple child records (lets call it Opportunity Child). On my Opp record I have a rollup field (new_rollup) that SUMs all the Opp children (new_childAmount). Every time that I create/update/delete a child the parent field should be recalculated, for this I'm using CalculateRollupFieldRequest. It works perfectly for Update and Delete, but when is the Create message it recalculates but it doesn't get the just added record.
Do you guys know if the Create message works differently for CalculateRollupFieldRequest?
BTW, the plugin step is registered for post-op for both Create and Update.
Code is below for reference:
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
var entityName = "new_childentity";
if (context.PrimaryEntityName != entityName) return;
var entity = context.MessageName != "Delete"
? (Entity)context.PostEntityImages["Target"]
: (Entity)context.PreEntityImages["Target"];
if (entity != null && entity.Contains("new_opportunityid")
&& entity["new_opportunityid"] != null)
{
var opportunity = (EntityReference)entity["new_opportunityid"];
var calculateRequest =
new CalculateRollupFieldRequest { Target = opportunity, FieldName = "new_rollup" };
var calculateResult = (CalculateRollupFieldResponse)service.Execute(calculateRequest);
var recalculatedOpportunity = calculateResult.Entity;
var calculatedAmount =
recalculatedOpportunity.GetAttributeValue<Money>("new_rollup").Value;
tracingService.Trace("New value: " + calculatedAmount);
}
*This post is locked for comments
I have the same question (0)