Pages

Wednesday 29 May 2013

Calculate total amount in subgrid records in crm 2011

The following javascript code will calculate all subgrid records total amount


function updateTotalAmount() {
   if(Xrm.Page.ui.getFormType()==2){
        setTimeout(SubGridLoadWait, 1000);
   }
}
function SubGridLoadWait()
{
var TotalAmount=0;
var array = [];
var gridControl = document.getElementById('accountContactsGrid').control;
var ids = gridControl.get_allRecordIds();
 for (i = 0; i < ids.length; i++) {
        var cellValue = gridControl.getCellValue('creditlimit', ids[i]);
        var n=parseFloat(cellValue.toString().replace(/[^0-9-.]/g, " "));
        TotalAmount=TotalAmount+n;
    }
   
}
function subgridOnload()
{    
setTimeout("SubGridRefresh();", 2500);
}  
function SubGridRefresh()
{    
var grid = document.getElementById("accountContactsGrid");
    if (grid) {       
        grid.attachEvent("onrefresh", ReLoadForm); 
     }

function ReLoadForm()
{  
 window.location.reload(true);
}


Then just put the "updateTotalAmount" and "subgridOnload" functions in form onload events.then publish.