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)

Summing quote sub grid

(0) ShareShare
ReportReport
Posted on by

I'm trying to sum a custom field in quote entity the custom field is called new_itemweight. I have tried the below code but can't seem to get my head round it. Any guidance or help would be greatly appreciated

I have added the steps in the plugin reg for Post-create, Postupdate 

My fields and entities are as follows

Parent Ent -quotes

Child - quotedetailsgrid

Column with in gris is - new_Itemweight

Field on parent for where i want the data is - new_totalweight

This is the code i'm trying to use

   public class SumWeight : IPlugin

   {

       public void Execute(IServiceProvider ServiceProvider)

       {

           IPluginExecutionContext Context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));

           IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));

           IOrganizationService Service = ServiceFactory.CreateOrganizationService(Context.UserId);

           if (Context.PostEntityImages.Contains("PostImage") && Context.PostEntityImages["PostImage"] is Entity)

{

               Entity Weight = (Entity)Context.InputParameters["Target"];

               var WeightTot = (EntityReference)Weight.Attributes["quotedetailsGrid"];

               decimal Total = FetchResult(WeightTot.Id, Service);

               // Updating Parent Entity

               Entity Quote = new Entity("quote");

               Quote.Id = Weight.Id;

               Quote["TestWeight"] = 10;

               Service.Update(Quote);

           }

       }

       private static decimal FetchResult(Guid quantity, IOrganizationService service)

       {

           string value_sum = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>

<entity name='quotedetailsGrid'>

<attribute name='ItemWeight' alias='ItemWeight_sum' aggregate='sum'/>

<filter type='and'>

 <condition attribute='ItemWeight' operator='eq' value='{0}' />

 </filter>

 </entity>

 </fetch>";

           decimal TotalValue = 0;

           value_sum = string.Format(value_sum, quantity);

           EntityCollection value_sum_result = (EntityCollection)service.RetrieveMultiple(new FetchExpression(value_sum));

           foreach (var c in value_sum_result.Entities)

           {

               decimal aggregate2 = ((int)((AliasedValue)c.Attributes["ItemWeight_sum"]).Value);

               TotalValue = aggregate2;

           }

           return TotalValue;

       }

   }

}

may thanks for your help

Cheers

Dan

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    I can't seem to get it to work. It did cross my mind but it never seems to give me the option to do it.  

  • Suggested answer
    Community Member Profile Picture
    on at

    If I understand you question correctly, you want

    1. Parent entity: quotes  rollup field: new_totalweight

    2 Child entity :  quotedetailsgrid field:  new_Itemweight

    First, I strongly agree with Guido you should use rollup field if your CRM are CRM 2015 or higher.

    In your case:

    quotes is the source entity, new_totoalwieght is the rollup field.

    quotedetailsgrid is the related entity, you can enable filter as new_Itemweight>0, then sum all of new_Itemweight to the rollup field.

    But if you want to try with plugin, the code you are using now is registered in quotes entity,which I am not sure how it works.

    Based on my understanding, the plugin should register on quotedetailsgrid entity.

    When this child entity create or the new_itemweight update, trigger the plugin.

    Use the parent lookup entity attribute get the quotes id and retrieve all the associated quotedetailsgrid's new_itemweight if value>0,  sum all the retrieved records and update new_totalweight in quotes.

  • Community Member Profile Picture
    on at

    Hi Thank you for your reply.

    I have had another look at the rollup option but when i got to create it the quotedetailsgrid is not an option. It is not an entity it's just a data grid on the quotes form. This is also the issue when registering the plugin the option for the quotedetailsgrid entity does not exist. It's really starting to bug me now lol. I've created plugin's to build massive pricelists from the smallest amount of data among a few things but with this one i'm lost so really appricate all the help you have given me

    Regards

    Dan

  • Community Member Profile Picture
    on at

    I've tried changing tack and gone for a JS option but i seem to have an issue and it chucks up an error. I founf the code from someone else doing the same as i was trying to do. The code is as follows

    function setupGridRefresh() {

    var targetgrid = document.getElementById("quotedetailsGrid");

    // If already loaded

    if (targetgrid.readyState == 'complete') {

       targetgrid.attachEvent("onrefresh", subGridOnload);

    }

    else {

       targetgrid.onreadystatechange = function applyRefreshEvent() {

           var targetgrid = document.getElementById("quotedetailsGrid");

           if (targetgrid.readyState == 'complete') {

               targetgrid.attachEvent("onrefresh", subGridOnload);

           }

       }

    }

    subGridOnload();

    }

    function subGridOnload() {

    //debugger;

    var grid = Xrm.Page.ui.controls.get('quotedetailsGrid')._control;

    var sum = 0.00;

    if (grid.get_innerControl() == null) {

       setTimeout(subGridOnload, 1000);

       return;

    }

    else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {

       setTimeout(subGridOnload, 1000);

       return;

    }

    var ids = grid.get_innerControl().get_allRecordIds();

    var cellValue;

    for (i = 0; i < ids.length; i++) {

       if (grid.get_innerControl().getCellValue('sb_itemweight', ids[i]) != "") {

           cellValue = grid.get_innerControl().getCellValue('sb_itemweight', ids[i]);

           cellValue = cellValue.substring(2);

           cellValue = parseFloat(cellValue);

           sum = sum + cellValue;

       }

    }

    var currentSum = Xrm.Page.getAttribute('sb_totalowe').getValue();

    if (sum > 0 || (currentSum != sum && currentSum != null)) {

       Xrm.Page.getAttribute('sb_totalowe').setValue(sum);

    }

    }

    The error that it brings when i load the form is Cannot read property 'readyState' of null at setupgridrefresh

    I have double check to make sure all the names of fields are correct and can't find any issues.

    Am i doing something silly here i believe this code was for 2013 have any of the principles changed

    Cheers

    Dan

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