Calculated Field on CRM Entity

Calculated Field on CRM Entity

  1. function setupGridRefresh() {
    var targetgrid = document.getElementById("NAME OF SUBGRID");
    
    // If already loaded
    if (targetgrid.readyState == 'complete') {
        targetgrid.attachEvent("onrefresh", subGridOnload);
    }
    else {
        targetgrid.onreadystatechange = function applyRefreshEvent() {
            var targetgrid = document.getElementById("NAME OF SUBGRID");
            if (targetgrid.readyState == 'complete') {
                targetgrid.attachEvent("onrefresh", subGridOnload);
            }
        }
    }
    subGridOnload();
    }
    
    function subGridOnload() {
    //debugger;
    var grid = Xrm.Page.ui.controls.get('NAME OF SUBGRID')._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('FIELD NAME LOWER CASE', ids[i]) != "") {
            cellValue = grid.get_innerControl().getCellValue('FIELD NAME LOWER CASE', ids[i]);
            cellValue = cellValue.substring(2);
            cellValue = parseFloat(cellValue);
            sum = sum + cellValue;
        }
    
    }
    
    var currentSum = Xrm.Page.getAttribute('DESTINATION FIELD').getValue();
    if (sum > 0 || (currentSum != sum && currentSum != null)) {
        Xrm.Page.getAttribute('DESTINATION FIELD').setValue(sum);
    }
    }

You need a field on the Account record which is updated every time you:

  • create a greatpeople.me  Portfolio record
  • update the value in a Portfolio record
  • delete a Portfolio record
  • re-parent a Partfolio record from one Account to another