Rollup fields are a great addition to Dynamics CRM. Rollup fields are calculated by Asynchronous System Jobs which runs in background. By default, Mass Calculate Rollup Field job runs every 12 hours after a Rollup field is created or updated. This delay is needed to assure that Mass Calculate Rollup Field job runs during the non-operational hours of an organization.

But in some business scenarios, users need this calculation in real-time and on demand. Dynamics CRM has an option available, called Online Recalculation. If a user hovers over the rollup field on the form, one can see the last time this field was updated, plus a Refresh icon to recalculate a rollup field. 

The following figure shows Online Recalculation:

   picture1

 

picture2

 

Limitations with Online Recalculation

  • The Online Recalculation option is only available in Online Mode. You can’t use it while working offline.
  • The Maximum Hierarchy Depth is limited to 10 for the source record. This limit does not apply when the field is recalculated by the system jobs.
  • The maximum number of records for a rollup refresh is limited to 50,000. This applies to related records across the hierarchy.
  • A User needs Write privileges on the entity and Write access rights on the source record.

What if the user only has Read privileges on the entity? And a user also needs this option available in Offline mode. For such scenarios, we have to use CalculateRollupFieldRequest available in Dynamics CRM SDK. This can be used in any custom code e.g. plugin, custom workflow activity or Web API call.

For one of our clients, we implanted the same solution using a custom plugin and a ribbon button. A “Rollup Amounts” button is created on Opportunity, which updates a field on the Opportunity form and saves the record to trigger this custom plugin. Here is the implementation in the custom plugin for

 

Implementation in the custom plugin for CalculateRollupFieldRequest

picture3

 

Two attributes of CalculateRollupFieldRequest

  1. Target – the target entity record, for which we need to calculate rollup field.
  2. FieldName – the field we need to calculate

IOrganizationService.Execute() method can be used to execute this type of request. And after a successful execution, we can have the updated value for the Rollup field, using CalculateRollupFieldResponse object.

 

picture4

 

References

https://technet.microsoft.com/en-us/library/dn832162.aspx

https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.calculaterollupfieldrequest.aspx