Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CalculateRollupFieldRequest and Create message on Child record

(0) ShareShare
ReportReport
Posted on by 95

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

  • Cleiton Santos - KFF Profile Picture
    95 on at
    RE: CalculateRollupFieldRequest and Create message on Child record

    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);
                }
            }


  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: CalculateRollupFieldRequest and Create message on Child record

    It looks like that your code doesn't work properly. Can you please post your code that uses Target from InputParameters for Create scenario?

  • Cleiton Santos - KFF Profile Picture
    95 on at
    RE: CalculateRollupFieldRequest and Create message on Child record

    [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 :)

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: CalculateRollupFieldRequest and Create message on Child record

    Another one idea - try to change plugin to Async. Theoretically it can work incorrectly because of transactions, dirty reads e.t.c.

  • Cleiton Santos - KFF Profile Picture
    95 on at
    RE: CalculateRollupFieldRequest and Create message on Child record

    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.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: CalculateRollupFieldRequest and Create message on Child record

    Hello,

    It works the same. Just use context.InputParameters["Target"] instead of image in Create scenario.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans