Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

I want to populate all the records sum of total amount on parent account from child record after update total amount value

Posted on by 5

 two entity 1. entitlement 2. Work Order entity 1is parent of entity 2 create below a field on entity 1 1. Account name 2. Coverage amount 3. balance amount 4. claim charges 5. Entitlement name creates below fields on entity 2 1.repair plan (lookup of entitlement entity ) 2. total amount 3. Balance 4. Coverage 5. work order title 6. System status option set(pending, scheduled, work completes) 7. Sub status- option set(invoice pending, the invoice generated) If work order system status is completed and sub status is invoice generated and user-modified total amount field then it should update sum of all work order total amount associated with entitlement on claim charges field of entitlement entity.

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    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:

    pastedimage1631509588678v1.png

    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.

  • Gowtham89 Profile Picture
    Gowtham89 138 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hi Pradeep,

    Thank you so much for taking your time and providing the Code. This is the scenario for which the plugin is required.

    I have an entity called 'Opportunity' and I have related entity called 'Service Products' in a 1 : N relationship. One opportunity can have multiple products. I have created a calculated field within the 'Service Products' entity called 'Total Amount' which will have total amount of the particular product. 

    I have currency field called 'Total Actual Value' within the Opportunity entity. So I need the currency field 'Total Actual Value' within the Opportunity entity to be populated with the sum of 'Total Amount' value from each products under an Opportunity.

    Product A Total Amount - $20

    Product B Total Amount - $ 40

    Then the Opportunity Total Actual Value Should show $60. Tomorrow if a new product gets added to the opportunity then the calculation should be done accordingly to include that product as well.

    I have followed the links and I have copied the above code but I am getting errors when executing the code. I am sure I am doing something wrong since this is my first time with C# plugin.

    Thanks,

    Gowtham

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hi Gwotham,

    We can achieve same with power automate but the power automate works in async way so it will not update the value instantly.

    So, for instant behaviour go with plugin approach.

    You can find below blogs which will help you to create plugin.

    https://docs.microsoft.com/en-us/powerapps/developer/data-platform/tutorial-write-plug-in

    https://www.tutorialspoint.com/microsoft_crm/microsoft_crm_plugins.htm

    Just to ease your work:

    Once you go through with above link then create one plugin library and in .CS file add below 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;
                    }
    
                    tracing.Trace("Execute function ended");
    
    
                }
                catch (Exception ex)
                {
    
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
    
    
            private void RefreshRollupFields(ITracingService tracing, IOrganizationService service, Entity workorderEnt)
            {
                tracing.Trace("Inside RefreshRollupFields");
    
                //add logical name of 
                string[] fieldList = { "" };
    
                if (workorderEnt != null && workorderEnt.Attributes.Contains(""))
                {
                    EntityReference entitlementObj = workorderEnt.GetAttributeValue("");
    
                    if (entitlementObj != null)
                    {
                        tracing.Trace("entitlement lookup found and ID is "   entitlementObj.Id);
    
                        for (int i = 0; i < fieldList.Length; i  )
                        {
                            tracing.Trace("Calculating for "   fieldList[i]);
                            string EntityLogicalName = entitlementObj.LogicalName;
                            Guid EntityID = entitlementObj.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");
            }
        }
    }
    


    Thanks,
    Pradeep. 

    Please mark this as VERIFIED if it helps.

  • Gowtham89 Profile Picture
    Gowtham89 138 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hi Pradeep,

    Thanks for the quick response again. I am sorry I should have been more brief before, I am new to D365 and I never had experience of writing plugin. That is the reason I am trying to find out ways to gets this done using D365 customization or using Power Automate Flows.

    Kindly advise.

    Regards

    Gowtham

  • Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    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,

    When we write plugin then that plugin(Keep Plugin Sync) will update the value instantly.

    The CalculateRollupFieldRequest obj will update the field instantly.

    Thanks,

    Pradeep.

  • Gowtham89 Profile Picture
    Gowtham89 138 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hi Pradeep,

    Thanks for the response. But I am looking for the Total value to be populated instantly in Parent entity field whenever the amount gets added or updated in related entities, in case of rollup I need to wait for schedule to be completed. So I am looking for ways to get this done using Power Automate.

    Kind regards,

    Gowtham

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hi,

    I have one more suggestion.

    We can also use the rollup field as shown below:

    pastedimage1631211780289v1.png

    Here, we need to take one additional steps:

    We need to write plugin on Work Order entity Create/Update/delete to update the rollup field value of Entitlement Entity.

    And to update the Rollup Field Value you can use below field:

    string EntityLogicalName = "entitlement";
    Guid EntityID = "";
    //rollup field logical name created on entitlment entity
    string fieldName="new_rollupfield";
     var calcularRollup = new CalculateRollupFieldRequest
    {
    Target = new EntityReference(EntityLogicalName, EntityID),
    FieldName = fieldName
     };
    var calcularRollupResult = (CalculateRollupFieldResponse)service.Execute(calcularRollup);


    Thanks,
    Pradeep.

    Please mark this as VERFIED if it helps.

  • Gowtham89 Profile Picture
    Gowtham89 138 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hello, I am also having a similar requirement where I need to update the sum of all related records and update the total in the parent entity field. I would like to achieve this using Power Automate...Is it possible to let me know how can it be done please?

  • Trail Account Profile Picture
    Trail Account 5 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    yes I want to do it through a plugin

    if possible please provide steps

    thanks

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: I want to populate all the records sum of total amount on parent account from child record after update total amount value

    Hi Trail Account,

    For that purpose you can use JS, plugin, custom workflow activity or power automate. These all are the options for you. Use whatever you want. Is this want ?

    or Do you want a complete step-by-step guide by using any of the above?

    Thanks

    Regards,

    Abdul Wahab

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans