CRM 2013 - calculated total from related entity and update parent entity RRS feed

  • Question

  • HI all,

    I need some help regarding calculating totals from related entity and update the parent entity.

    J have created a custome entity Product_Items which is related to Opportunity.

    I have a sub-grid on my Opportunity form which displays the Product_items and i hve a total field on my Opportunity form.

    What i want is whenever a Product_items is added, updated or deleted, a totals of all related Product_item is calculated and displayed in the field .

    Can anyone help me how to achive this?

    Many thanks



    Sunday, December 8, 2013 7:02 PM


All replies

  • Hello,

    The best way is to develop a plugin that will handle Create/Update/Delete messages for Product_items entity that will execute recalculation of totals and update opportunity. Recheck following article - http://mscrmshop.blogspot.co.uk/2012/04/how-to-update-parent-record-when-child.html

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC

    My blog
    Sunday, December 8, 2013 7:19 PM
  • Hi Andrii,

    I have develop a plugin to update opporunity when a product_item is being creatd, it work fine.

    But when deleting a Product_Item, the Opportunity do'nt get updated.

    One more issue, the Opportunity form do'nt get refreshd when a product_item is added.  I have to re-open the form for the total to get displayed.

    Find below my code for delete: 

     IPluginExecutionContext Context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService Service = ServiceFactory.CreateOrganizationService(Context.UserId);

                ITracingService tracer = (ITracingService)ServiceProvider.GetService(typeof(ITracingService));
                tracer.Trace("ENTER: ");

                if (Context.InputParameters.Contains("Target") && Context.InputParameters["Target"] is Entity)
                    Entity entProductLines = (Entity)Context.InputParameters["Target"];

                    //Getting Opportunity GUID
                    Guid gIDOpportunity = entProductLines.Id;
                    //Guid gProductLines = (Guid)entProductLines.Attributes["new_serviceproductlineid"];
                    EntityReference gProductLinesE = (EntityReference)entProductLines.Attributes["new_serviceproductlineid"];
                    Guid gProductLines = gProductLinesE.Id;

      Is the code ok, should it be Context.InputParameters.Contains("Target")?

    Any help will be greatly appreciated.

    Many Thanks



    Monday, December 9, 2013 9:57 AM
  • Hi Vinay,

    Target parameter is not same in every plugin, it depends on message type, have look into input messages here

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, December 9, 2013 10:19 AM
  • HI,

    One more issue.

    Can anyone guide me how to get my Opportunity form refreshed when the related Product_Items get added in the Sub Grid?

    Many Thanks



    Tuesday, December 10, 2013 5:25 AM