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
The code doesn't change too much
public void Execute(IServiceProvider serviceProvider) { 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; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) return; var entity = context.MessageName.ToLower() != "delete" ? (Entity)context.InputParameters["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); } }
It looks like that your code doesn't work properly. Can you please post your code that uses Target from InputParameters for Create scenario?
[quote user=""Andrii"][/quote]
Another one idea - try to change plugin to Async. Theoretically it can work incorrectly because of transactions, dirty reads e.t.c.
I've tried that already and no dice :)
Another one idea - try to change plugin to Async. Theoretically it can work incorrectly because of transactions, dirty reads e.t.c.
I was using context.InputParameters["Target"] and it had the same behavior, that's why I tried to change to image to give it a try but it didn't work either.
Hello,
It works the same. Just use context.InputParameters["Target"] instead of image in Create scenario.
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6