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


 

The on demand and automated way

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.

Creating the CalculateRollupField Custom Connector

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

  1. In PowerApps maker studio expand the Data node and click Custom Connector. This opens the Custom Connector list.
  2. Click New custom connector and select Create from blank. This starts the four step guide for creating a custom connector
  3. On the General information screen fill in the Host and Base URL fields as below and click on to the Security screen
     

    Create Custom Connector - General Information


     
  4. On the Security screen select OAuth 2.0 as the Authentication type.
     
  5. In the OAuth 2.0 form select Azure Active Directory and fill in the form with the details from your app registration in Azure Active Directory as in the image below and click on to the Definition screen:
     

    Create Custom Connector - Security Settings

     
  6. Click New action and as a minimum give your new action an Operation ID

    Create Custom Connector - Action General Information

     
  7. To define the properties of the action click Import from sample

    Create Custom Connector - Import from sample

     
  8. In the Import from sample pane select the GET verb and paste in the following URL pointing to your own environment. NOTE, you can leave the entity, record id and fieldname as is. These parameters will be configurable when using the custom connector.
    https://[YOUR_ORG].crm4.dynamics.com/api/data/v9.1/CalculateRollupField(Target=@target,FieldName=@fieldname)?@target={'@odata.id':'[your_entity]([your_recordid])'}&@fieldname='[your_fieldname]'
     

    Create Custom Connector - Import Sample Request

     
  9. Click Import to create the new Action and then click on to the Test screen
  10. Before you can test the custom connector you need to click the Create connector button. Once the connector has been created you can go ahead and test it.
  11. Click new Connection on Connection form

    Create Custom Connector - Test Connection

     
  12. Login in with valid credentials for your environment

    Create Custom Connector - Select Credentials for test

     
  13. In the @target parameter input the following
    {'@odata.id':'[your_entity]([your_recordid])'}
  14. In the @fieldname parameter input the following including the single quotation marks (')
    '[fieldname]'

    Create Custom Connector - Test parameters

     
  15. Your output should look something like in the image below

    Create Custom Connector - Test output

Create a Power Automate Flow to calculate your rollup fields on demand

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