web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

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

  • Cleiton Santos - KFF Profile Picture
    95 on at

    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

    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

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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

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


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans