Trigger Rollup Field Recalculation using Dynamics 365 WebAPI and JavaScript

Dominic Jarvis, 07 May 2018

A common operation that is performed when working with records in Microsoft Dynamics 365 is rollup field recalculation. This is because by default, rollups are only recalculated once every hour. If you have logic that requires you to present the result of a rollup field immediately (for reporting purposes, or verification purposes), it may be beneficial to recalculate the rollup field on demand in order to have the latest data.

image

This operation can be performed with the WebAPI using a built in ‘Function’. The documentation for this function can be found here:

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/calculaterollupfield?view=dynamics-ce-odata-9,

and the documentation for performing functions can be found here:

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/use-web-api-functions?view=dynamics-ce-odata-9.

For the purposes of the following examples, we’ll be recalculating the number of attendees for an event that we are hosting. The entity will be Event (mag_event), with the field being Number of Attendees (mag_numberofattendees).


The CalculateRollupField function is an unbound function, and takes two parameters – Target and FieldName. The basic syntax for performing an unbound function is as shown below.

  • GET [Organization URI]/api/data/v9.0/FunctionName(Param1=@p1,Param2=@p2)?@p1='p1 data'&@p2=123456 

Because the Target parameter of the CalculateRollupField function is of type crmbaseentity, this must be bound to an existing entity using the @odata.id attribute. Therefore, our function ends up looking as follows:

  • GET [Organization URI]/api/data/v9.0/CalculateRollupField(Target=@tid,FieldName=@fn)?@tid={'@odata.id':'mag_events(9f3162f6-804a-e611-80d1-00155d4333fa)'}&@fn='mag_numberofattendees'

When performed with JS, the request ends up looking like the following:

var requestUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/CalculateRollupField(Target=@tid,FieldName=@fn)?@tid={'@odata.id':'mag_events(9f3162f6-804a-e611-80d1-00155d4333fa)'}&@fn='mag_numberofattendees'"

var req = new XMLHttpRequest();
req.open("GET", requestUrl, false);
req.send();

Note that when the value is recalculated, it is not automatically refreshed on open forms. These forms will need to be refreshed manually, so this function is mostly useful when updating rollups from related entities.