Rollup fields are a great feature of the PowerPlatform. Rollup fields are used to aggregate values of related records to give the user insights into data in the record relationships. They can be used to calculate the total amount of an order by aggregating the order line amounts using the SUM() function. Another common scenario is using the COUNT() function to count number of related records. This can also be done using hierachical relationships, ie. aggregate values from related records and their related records and their related records ...
Configure Rollup Field
You can imagine how much work is done behind the scenes in order to achieve the desired goal based on the simple configuration. It would require a lot of work to implement this functionality yourself.
However, as with most great things in life, there is a catch.
Rollup fields are only calculated every 12 hours via a recurring system job. The schedule of the job can be changed but the frequency cannot.
Users can manually force a recalculation of a Rollup field by clicking the Recalculate button below the rollup field.
Manually recalculate rollup field
There is a third way to Recalculate Rollup fields. Using the PowerPlatform Web API it is possible to Recalculate via the CalculateRollupField function (https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/calculaterollupfield?view=dynamics-ce-odata-9). So, how do you go about calling this function on demand in an automated way. You use Power Automate to create a Flow that calls a CalculateRollupField Custom Connector of course.
NOTE, in order to follow the below steps you should already have a valid app registration in your Azure Active Directory. If you don't have an app registration for your PowerPlatform instance you can create one by following this guide https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/walkthrough-register-app-azure-active-directory
Follow these steps to create the CalculateRollupField Custom Connector
Now that the custom connector has been created you should put it to use in Power Automate by creating a Flow.
For the purpose of demonstration I have created a little Model Driven app with two entities, Parent and Child. Child relates to Parent and the Parent entity has two rollup fields, Number of children and Average age of Children.
In order to create a flow to update the two rollup fields on the Parent entity I use the When a record is created trigger configured to fire when a new Child record is created.
Once triggered the flow has two actions, one for each rollup field. Both of them using the Calculate Rollup Field custom connector. You can see the flow configuraiton below
Use CalculateRollupField Custom Connector from Power Automate Flow