I have a function which runs on Onload event. In this form there is a subgrid. My task is to fetch a field value from all the subgrid rows and add them up and store in the amount field in the form. Currently, this function runs only on onload event but does not work for grid row add and delete. here is the code below. please guide.
// JavaScript source code00971561722265
function UpdateAmountfromSubgrid() {
var grid = function () {
var amount = 0;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/one_rates?$select=one_subtotalmaterialsell&$filter=_one_rateitems_value eq 6A52CC44-BD89-E711-8127-6C3BE5B3B698", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var one_subtotalmaterialsell = results.value[i]["one_subtotalmaterialsell"];
amount = amount + one_subtotalmaterialsell;
var one_subtotalmaterialsell_formatted = results.value[i]["one_subtotalmaterialsell@OData.Community.Display.V1.FormattedValue"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
Xrm.Utility.alertDialog(amount);
Xrm.Page.getAttribute("one_amount").setValue(amount);
};
Xrm.Page.getControl("RateItems").addOnLoad(grid);
}









