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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calculate sum of a column of a subgrid in an entity using custom script

(0) ShareShare
ReportReport
Posted on by

My Dynamics 365 CRM is in Version 9 and there is not enough reference on this issue I am facing. I am looking for a custom script to get a sum of a column of a subgrid in an entity. I have an entity with a field name "total_amount", on that entity , i have a form with a subgrid referenced to another entity, which possess the records where i have product lines in it. How do i sum up the price of the product line, and insert the sum into "total_amount" field?

Currently i'm trying to use the gridcontext to achieve this but there isnt proper reference as to how do i use it to extract the column value and total it up.

I found this code but its only for older version of CRM :


setTimeout(calcCostoTotal, 3000);
}
function calcCostoTotal() {
var grid = document.getElementById(‘Deals’);
var ids = grid.control.get_allRecordIds();
var sum = 0.00;
var cellValue;
for(i = 0; i < ids.length; i++) {
var cellValue = grid.control.getCellValue(‘NAME OF THE FILED THAT IS BEING SUMMED’,ids[i]);
var number = Number(cellValue.replace(/[^0-9\.]+/g,”));
sum = sum + number;
}
Xrm.Page.data.entity.attributes.get(“NAME OF THE FILED THAT THE SUM IS GOING TO”).setValue(sum);
}

*This post is locked for comments

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

    Hi Sean,

    The simple way is that you can handle through Plugin whenever new record created/updated to calculate the sum.

    still you want to go with the Javascript , you can move the code to subgrid onlaod Event and calculate the SUM.

    docs.microsoft.com/.../subgrid-onload

    https://youtu.be/0h15viM19Ic

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Write web API query passing the parent Guid of the record to subgrid entity and  prepare the sum of the columns.

    Have a look below reference similar requirement -

    community.dynamics.com/.../296630

    Use CRMRestBuilder to get the query -

    www.youtube.com/watch

  • Suggested answer
    naresh babu Profile Picture
    425 on at

    Try this below script:-
        Xrm.Page.getControl('salesorderdetailsGrid').getGrid().getRows().get(i).getData().getEntity().getAttributes().get('priceperunit').getValue();
    ________________________________________________

    function CostoTotal()
    {
    debugger;
    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("salesorderdetailsGrid") != null && Xrm.Page.getControl("salesorderdetailsGrid") != undefined) {
    var count = Xrm.Page.getControl("salesorderdetailsGrid").getGrid().getTotalRecordCount();
    alert("Total Products:"+count);
    for(i = 0; i < count; i++) {
    var gridControl = Xrm.Page.getControl('salesorderdetailsGrid').getGrid().getRows().get(i).getData().getEntity().getAttributes().get('priceperunit').getValue();
    //Xrm.Page.getControl('salesorderdetailsGrid').getGrid().getRows().get(0).getData().getEntity().getAttributes().get('priceperunit').getValue();
    alert(gridControl );
    // var sum = 0,00;
    var cellValue = gridControl;
    alert(cellValue);
    cellValue = cellValue.replace( /,/,"." );
    alert(cellValue);
    var sum = sum + parseFloat(cellValue);
    alert(sum);
    }
    }
    Xrm.Page.getAttribute('new_total').setValue(sum);
    }

  • Jian Qi Profile Picture
    10 on at

    I believe this is for the older version of crm.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans