CRM 2013 Calculate Totals from Subgrid

Hi Tech’ies,

I got a requirement to calculate the Total Value(a decimal field) from all the records inside a Subgrid (N:N Relationship) in CRM’13. I was thinking to do this, because CRM 2013 won’t support some functions such as document.getElementById but CRM 2011 will support this.

But, I tried this way and it worked well.

 

function getTotal(){
setTimeout(calcCostoTotal, 2000);
}

function calcCostoTotal() {

var grid = document.getElementById(‘CasualStaffForm’); //Name of the Subgrid
var ids = grid.control.get_allRecordIds();
var sum = 0.00;
var cellValue;

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

var cellValue = grid.control.getCellValue(‘new_total’,ids[i]);

//new_total is the decimal field in related entity(record inside subgrid)

var number = Number(cellValue.replace(/[^0-9\.]+/g,””));
sum = sum + number;
}

alert(sum);

}

 

Cheers,

Naren