Description - Dynamics CRM 365 has two types of calculated fields
1- Calculated fields - This type of field is only available on the table.
2- Roll-up fields - This type of field is use to calcuate the sum from the child entity. These fields recalculated at a certain time or by manually clicking on recalculate button.
Step 1 - Write a function below to calculate the Rollup Fields using JS
function funccalculateRollup(entityName, recordId, fieldName)
{
// Define the entity set name, record ID, and field name
// Create an XMLHttpRequest object
var req = new XMLHttpRequest();
req.open(this.method, this.url, true);
// Set the request headers
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
// Open the request with the GET method
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/CalculateRollupField(Target=@p1,FieldName=@p2)?" +
"@p1={'@odata.id':'" + entityName + "(" + recordId + ")'}&" +
"@p2='" + fieldName + "'", true);
// Define the callback function
req.onreadystatechange = function ()
{
if (this.readyState === 4)
{
req.onreadystatechange = null;
if (this.status === 200)
{
// Parse the response
var result = JSON.parse(this.response);
// Get the value of the rollup field
var value = result[fieldName];
// Do something with the value
console.log(value);
}
else
{
// Handle the error
console.error(this.statusText);
}
}
};
// Send the request
req.send();
}
Step 2
Call The above method
function calculateRollupForBilledTime(executionContext)// Pass form Context
{
var formContext = executionContext.getFormContext();
var entityName = formContext.data.entity.getEntityName();
var record = formContext.data.entity.getId();
var entitySetName = "incidents";
var recordId = record.replace("{", "").replace("}", "");
var FieldName = "sbnfu_billabletime";
funccalculateRollup(entitySetName, recordId, FieldName);
}