Quantcast
Channel: CRM Development 論壇
Viewing all articles
Browse latest Browse all 10280

Calculting Total Sum of Related Entity Fields.

$
0
0

I have 2 Entites, VehicleJobs and VehicleSupplies. This is a 1:N relationship.

A  VehicleJob will have many VehicleSuplies, each VehicleSuplpy has a cost field.

I am using a subgrid to show the related VehicleSupplies on the VehicleJobs Form.

I want to calculate the total sum of related VehicleSupplies and put it inside a field in the VehicleJobs Form. It must automatically update if related VehicleSupplies records are added or removed.

Here is what i have tried so far from another piece of code i found.


function setupGridRefresh() {
var targetgrid = document.getElementById("Parts");

// If already loaded
if (targetgrid.readyState == 'complete') {
    targetgrid.attachEvent("onrefresh", subGridOnload);
}
else {
    targetgrid.onreadystatechange = function applyRefreshEvent() {
        var targetgrid = document.getElementById("Parts");
        if (targetgrid.readyState == 'complete') {
            targetgrid.attachEvent("onrefresh", subGridOnload);
        }
    }
}
subGridOnload();
}

function subGridOnload() {
//debugger;
var grid = Xrm.Page.ui.controls.get('Parts')._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('c2_price', ids[i]) != "") {
        cellValue = grid.get_innerControl().getCellValue('c2_price', ids[i]);
        cellValue = cellValue.substring(2);
        cellValue = parseFloat(cellValue);
        sum = sum + cellValue;
    }

}

var currentSum = Xrm.Page.getAttribute('c2_totalcost').getValue();
if (sum > 0 || (currentSum != sum && currentSum != null)) {
    Xrm.Page.getAttribute('c2_totalcost').setValue(sum);
}
}

"Parts" = Unique Name of Subgrid which holds the related VehicleSupply records.

"c2_price" = The field in VehicleSupplies which shows the price of the supply.

"c2_totalcost" = The field in VehicleJobs where i want the totalsum to go.

However, i get an error. "Unable to get property 'ReadyState' of undefined or  null reference.







Viewing all articles
Browse latest Browse all 10280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>