Skip to content

CalculateRollupField with WebApi function in Javascript

Posted in Dynamics 365, Power Platform, and Revive

Microsoft added with Service Pack 1 a new function called “CalculateRollupField” in Dynamics CRM (v 8.1) which enables us to recalculate a rollup field on demand.
I will show you here how you can use it in Javascript with a http request against the WebApi.

The CalculateRollupField function inside the webrequest needs a few parameter to know which rollup field you want to to calulate:

CalculateRollupField in Javascript:

function calcRollupField(strTargetEntitySetName, strTargetRecordId, strTargetFieldName)
{
    strTargetRecordId = strTargetRecordId.replace("{", "").replace("}", "");
    var req = new XMLHttpRequest();
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" +
        "CalculateRollupField(Target=@p1,FieldName=@p2)?" +
        "@p1={'@odata.id':'" + strTargetEntitySetName + "(" + strTargetRecordId + ")'}&" +
        "@p2='" + strTargetFieldName + "'", true);

    req.onreadystatechange = function ()
    {
        if (this.readyState === 4)
        {            
            req.onreadystatechange = null;
            if (this.status === 200)
            {
                var results = JSON.parse(this.response);
            }
            else
            {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };    
    req.send(JSON.stringify({}));
}

The answer of the webservice for the CalculateRollupField function contains the value for the target field, the date of the last calculation and its state.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.