locked
How to sum subgrid column in CRM 2013 RRS feed

  • Question

  • Hi,

    I want to sum Subgrid column named "Quantity" and show in alert.

    Friday, December 26, 2014 9:10 AM

All replies

  • You can do it with JS

    I use XrmSvcToolkit (https://xrmsvctoolkit.codeplex.com/) to query values:

    var EntityId = Xrm.Page.data.entity.getId();
    var SubEntityName = "Product"; //Here put Entity Name of subgrid
    var SubEntityColumnName = "quantity" //Here put Column Name
    var SubEntityLinkColumnName = "productid" //Here put Column Name wich link sub entity and base entity
    
    
    XrmSvcToolkit.retrieveMultiple({
        entityName: SubEntityName,
        async: false,
        odataQuery: "$select=" + SubEntityColumnName + "&" +
            "$filter=" + SubEntityLinkColumnName + "/Id eq (guid'" + EntityId +"')",
        successCallback: function (result) {
            for (var summ = 0, k = result.length; k; summ += result[--k].quantity);
            alert(summ);
        },
        errorCallback: function (error) {
            alert(error);
        }
    });



    • Proposed as answer by xjomanx Friday, December 26, 2014 11:32 AM
    • Edited by xjomanx Friday, December 26, 2014 11:39 AM
    Friday, December 26, 2014 11:32 AM
  • Hi,

    You can also try this code to calculate subgrid amount total for crm 2013.

    Add your subgrid name in place of "testing"  and amount field name in place of 'new_amount'.

    function RetrieveSubGridRecords() {
        if (document.getElementById("testing")) {
            var grid = document.getElementById("testing").control;
            var ids = grid.get_allRecordIds();
            var sum = 0.00;
            var cellValue;
           for(i = 0; i < ids.length; i++) {
               var cellValue = grid.getCellValue('new_amount',ids[i]);
               var number = Number(cellValue.replace(/[^0-9\.]+/g,""));
               sum = sum + number;
            }
           alert('s'+sum);
        }
        else {
           setTimeout("RetrieveSubGridRecords();", 2500);
        }
    }

    Hope this help..
    Thanks



    • Proposed as answer by Suresh Sorde Friday, December 26, 2014 12:30 PM
    • Edited by Suresh Sorde Friday, December 26, 2014 12:52 PM
    Friday, December 26, 2014 12:30 PM
  • Hi Suresh,

    Your above code works fine for summing the sub grid. Is there a way to achieve the same in home grid(in a view). for example i will have a button at the top of the home grid, on clicking that, the particular field's sum of all the records or selected records has to be alerted.

    I referred this link but no desired output for me. 

    Any help on this please.

    Regards,

    Rekha.J

    Monday, December 29, 2014 10:08 AM
  • Hi Rekha,

    I have not tried for such requirement. 

    I think you can easily achieve this by creating custom report with same format of your grid or using charts.

    Also raise new question with your tried code. It will help you to easily get answer of your question.

    Thanks


    Monday, December 29, 2014 1:44 PM
  • Hi Suresh,

    Thanks for the reply. We have the reports of this way , but the requirement now is to sum in the view itself.

    I've had already raised a question but unfortunately no relevant reply.

    Anyways thanks for your reply.

    Regards,

    Rekha.J

    Tuesday, December 30, 2014 5:44 AM
  • Hi Rekha,

    What issue you are facing ??


    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our 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.

    Tuesday, December 30, 2014 7:35 AM
    Moderator
  • Hi Mahender, 

    The error was at these lines while debugging the code.

    var headers = gridBar.getElementsByTagName('TH'); 

    Regards,

    Rekha.J

    Tuesday, December 30, 2014 8:38 AM
  • But why you are using getElementByTagName, you can pass parameter to your ribbon button and get record based on that id and do some of your required column ?

    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our 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.

    Friday, January 2, 2015 4:17 AM
    Moderator