CRM Crate

Programmatically trigger a Rollup field in Dynamics CRM

In this course, we will learn how to trigger a roll up field with help of a plugin or workflow code in Dynamics 365 CRM. Before we start, make sure to go through the below courses.

How to programmatically trigger a Roll Up field in Dynamics CRM?

What is a roll up field in Dynamics 365?

Rollup fields are a one of the great feature provided by Microsoft in Dynamics CRM. With help of Roll Up field we can perform aggregation of data / records from many child records to an eventual Parent record. Similar to the calculated fields, you can create a rollup field by adding a new field to your entity, choose the field type of Rollup, choose your data type, and build your logic in the same manner as a business rule or a calculated field.

Roll up field refresh frequency

For each entity that contains a rollup field, a recurring system job is automatically created to asynchronously calculate the rollup. 

  • Navigate to Settings > System Jobs.
  • Change the view to Recurring System Jobs.
  • Look for jobs using the Calculate rollup fields for the <entity name> entity naming convention.

If you would like to change the recurrence of an entity’s Roll Up calculation, click into the job and select Modify Recurrence under More Actions. The maximum recurrence setting is 1 hour.

Changing the Rollup Calculation Frequency 1

Programmatically triggering the Roll up field using a plugin code

We can trigger the Roll Up field using a plugin code or a custom workflow. With help of CalculateRollupFieldRequest we can sent a request to refresh any particular Roll Up field, this eliminates the dependency of manually refreshing a Roll Up field and we can refresh the Roll Up field as per our requirement.

Below is the plugin / custom workflow code

try
            {
			
			//Declaring Variables Which Contains Rollup Field Schema Name, CRM Service and Parent Entity Entity Refrence ID.
			string FieldName = "totalamount"
			IOrganizationService service = factory.CreateOrganizationService(context.UserId);
			string EntityLogicalName = "account";
			Guid EntityID = "8676f06d-7001-eb11-a813-000d3a8b66de";
		
		    //Sending A Rollup Refresh Request Using An API.
                var calcularRollup = new CalculateRollupFieldRequest { 
                    Target = new EntityReference(EntityLogicalName, EntityID), FieldName = FieldName 
                };
                var calcularRollupResult = (CalculateRollupFieldResponse)service.Execute(calcularRollup);
            }
            catch (Exception e)
            {
                AddTracing($"Error Occurred :{ e.Message}");
            }

Below is the explanation of above code snippet

  • We will need the variable information such as Rollup field schema name, CRM Service for carrying out operations, logical name of the entity in which the Rollup field is placed, GUID of target record (Record which triggered the plugin / workflow).
  • Pass the above information are create an object of CalculateRollupFieldRequest.
  • Using the CRM Service, send a Rollup field refresh request.

Conclusion

Thus we learned how to refresh the Rollup field programmatically using Plugin or Custom workflow.

CRM Crate

All In One Platform For Learning Microsoft CRM.

2.7 3 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.