Wednesday, June 17, 2020

Generic way to Refresh Rollup field in Dynamic CRM

Opportunity Roll-up fields




Opportunity Products Grid




Roll-up fields Definition





Generic method to re-calculate and refresh the Rollup field.

refreshRoleupField: function (executionContext, entityName, entityId, rollup_fieldName)
{
 debugger;
 var formContext = executionContext.getFormContext();           
 var clientUrl = formContext.context.getClientUrl();

 // Method Calling and defining parameter
 var rollupAPIMethod = "/api/data/v9.1/CalculateRollupField(Target=@tid,FieldName=@fn)";

 // Passing Parameter Values
 rollupAPIMethod += "?@tid={'@odata.id':'" + entityName + "(" + entityId + ")'}&@fn='" +   rollup_fieldName + "'";

 var req = new XMLHttpRequest();
 req.open("GET", clientUrl + rollupAPIMethod, false);
 req.onreadystatechange = function ()
 {
    if (this.readyState === 4) 
    {
        req.onreadystatechange = null;
        if (this.status === 200) 
        {
          console.log("Field Recalculated successfully");                       
        }
    }
 };

 req.send();           
 formContext.data.entity.save();
}

Syntax for calling the method

onLoad: function (executionContext)
{
    var entityId = formContext.data.entity.getId().replace('{''').replace('}''');
    opty.Functions.refreshRoleupField(executionContext, 'opportunities', entityId, 'new_totalunitcost');
    opty.Functions.refreshRoleupField(executionContext, 'opportunities', entityId, 'new_sumextendedcost');

}

I hope you find this useful.

Thanks.
Vipin



---------------------Refer other blog post here---------------------

Most common JavaScript methods for Dynamic 365 CRM