DYNAMICS 365 HOW TO FORCE RECALCULATE ROLLUP FIELDS
In this quick Blog post, we will see how to force recalculate Rollup fields in JavaScript and C# using the below functions.
-
Recalculate Rollup field in JavaScript (for account entity)
function reCalculateRollupField(entityPluralName, recordGuid, rollupFieldName) {
recordGuid = recordGuid.replace("{", "").replace("}", "");
var requestUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/CalculateRollupField(Target=@tid,FieldName=@fn)?@tid={'@odata.id':'" + entityPluralName + "(" + recordGuid + ")'}&@fn='" + rollupFieldName + "'";
var HttpReq = new XMLHttpRequest();
// Double Check the API URL
HttpReq.open("GET", requestUrl, true);
HttpReq.onreadystatechange = function () {
if (this.readyState === 4) {
HttpReq.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
HttpReq.send(JSON.stringify({}));
}
-
Recalculate Rollup field in C# (for account entity)
public void RecalculateRollupField(Guid accountGuid, string rollupFieldName)
{
CalculateRollupFieldRequest request = new CalculateRollupFieldRequest
{
Target = new EntityReference(Account.EntityLogicalName, accountGuid), //Record to be updated
FieldName = rollupFieldName //Rollup Field Name
};
CalculateRollupFieldResponse response = (CalculateRollupFieldResponse)AdminService.Execute(request);
}
Hope This Helps!
Comments
Post a Comment