Rollup fields are one of the most exciting new features that were introduced in recent years that significantly reduced the amount of custom code required. Rollup fields can aggregate values from other attributes. You can calculate sums, averages, minimums, maximums, and counts.
In this recipe, we will create a rollup field that counts how many active activities are associated with a contact.
Similar to the previous recipes, a System Customizer
or higher security role is required to perform the configuration as well as a solution to contain the changes.
- Navigate to
Settings
|Solutions
|Packt
. - Create a new field by clicking on
Entity
|Contact
|Fields
|New
and enter the following details:Display Name
:Number of Activities
Data Type
:Whole Number
Field Type
:Rollup
- Click on
Edit
next to theField Type
dropdown. - In the
Rollup Field
dialog, underRelated Entity
, selectActivities (Regarding)
and click the tick box. - In the
Filters
section, select the following:Activity Status
underField
Equals
underOperator
Value
underType
- Check
Open and Scheduled
underValue
- Click on the tick box.
- Under
Aggregation
, select the following:Aggregate Function
underCount
Activity
underAggregated Related Entity
field
- Click on the tick box. Your
ROLLUP FIELD
dialog will look as follows:
- Click on
Save and Close
on theROLLUP FIELD
dialog and the attribute dialogue. - Click on the
Publish All Customizations
button for your solution.
In step 2 to step 7, we created a new rollup field of type whole number that calculates the total count of active activities associated with a contact.
Note
A couple of things to note about rollup fields: the fields are read-only and the calculation does not take into account a user's security roles.
When a rollup field is created, behind the scenes, a system asynchronous job is scheduled to execute 12 hours after the field is created. Navigate to Settings
| System Jobs
; you will see a job of type Mass Calculate Rollup
field with the name of your new field in System Job Name
. You can force the job to run sooner by clicking on Action
| Postpone
and entering an earlier date/time. It is not recommended to do so when you have a large number of records in a production environment. The first time the job runs, it will have to update all fields, which might lead to a performance impact.
Subsequently, the System Jobs
option will have a Calculated Rollup
field per entity that has rollup fields configured. By default, subsequent rollup field calculations will take place every hour, (which is the fastest schedule). If an hourly update is not necessary, you can update the value by double-clicking on the System Job
option, and selecting Actions
| Modify Recurrence
. This will present a dialog box that helps you redefine the recurrence of the execution:
Note
Given that the rollup fields are executed as asynchronous jobs, if the calculation is not occurring, consider checking the status of the asynchronous service. For on-premise installation, log on to your backend server and check its status. For online implementations, log a support call with Microsoft to check the status of the service.
On top of the automated schedules, rollup fields can also be executed programmatically by executing CalcualteRollupFieldRequest
. Furthermore, if you search online for "rollup field workflow", you will find custom workflow activities built by the community to force recalculations in your configurable workflows.
Rollup fields are a powerful feature in Dynamics 365. On top of the counting capability described in this recipe, rollup fields offer other types of aggregations. Furthermore, in some cases (activities), the aggregation can span across multiple relationship levels.
Counting the number of records is one of the many capabilities of a rollup field. You can also get the maximum, minimum, average, and sum of a field, assuming the field type allows it. As you select a different aggregate function, the entity fields will be filtered accordingly.
Given that our rollup field is aggregating activities, the field can also include indirectly related activities by defining Activity Parties
(activities) under indirectly related activities. This will expand the aggregation, not only when the contact is in the regarding field, but also in the parties field.
For more details about rollup fields, visit https://technet.microsoft.com/en-nz/library/dn832162.aspx.