Among the many new features added in Dynamics CRM 2015, Rollup Fields is one of the interesting new feature which provides a way to perform a record level aggregation from child records to its parent record. This avoids plug-in development and can be easily achieved by end users through simple setup and settings itself without any code involved.
Let’s take an example to explore implementation and working of rollup fields. Suppose we want to show total count of all the active contacts associated with an active Account and its Child Accounts on Account form.
Rollup field setup:
To achieve this we will first start with creating new Rollup field for Account as shown below:
Here you can see that the field type which we have selected is of type “Rollup” and the data type which we have selected is of type “Whole Number”. The Data Type of the Rollup field must match the Data Type of the field for which we want to calculate the rollup. So if we want to show rollup of a field for Contact on Account form then the data type of rollup field must match the data type of the field for which we want to calculate rollup.
Also, you can see the “Edit” button besides “Rollup” selected Field Type. Whenever we select field type as “Rollup”, Edit button appears next to the Field Type and that brings up one editor screen which is similar to the “Business Rules” screen. Please refer the below screen:
Here we can specify the filters, conditions and aggregations which will be applied to the rollup field. The details are explained below,
- The source entity which is taken by default to be the entity for which the rollup field is created. In our example it is “Account” entity hence the “Source” is by default “Account”
- Use Hierarchy can be set to Yes/No for utilizing parent-child relationship. In our example we want to get aggregation for all Child Accounts of an Account hence we have set it to “Yes”
- We can apply filters for source entity using the “Filters” option where we can specify the conditions to apply. In our example we have specified the condition that the Account status needs to be “Active” so rollup happens for active records only.
- When we want to get aggregation of related entity data in our rollup then we specify the “Related Entity”. In our example since we want to show total count of all the active contacts present for an Account and its Child Accounts, we have selected “Contacts (Company Name)” as the “Related” entity.
- If we want to apply filters for related entity then we can do that by adding filter conditions in “Related Entity”. In our example since we want to show total count of all the active contacts present for an Account and its Child Accounts we have added filter condition to check if “Status” equals “Active” for related opportunities
- And finally we specify the aggregation function which we want to apply and for which field. In our example since we want to show all active Contacts for an Account, we have selected the aggregate function as “Count”.
User Interface:
The Rollup Field that we just created will be seen on the “Source Entity” form i.e. account form:
So the total Contacts for a particular Account will be calculated and shown in this “Total Contacts” Rollup field for that Account . If the Account has Child Accounts then the total of Contacts for that Account along with its Child Account’s Contacts will be calculated and shown in this “Total Contacts” Rollup field for that Account.
Rollup fields are read only. On this Rollup field you can see the “Refresh” icon attached to the Rollup field. Since rollup operates asynchronously, it is calculated on hourly basis. But we can also self-trigger the rollup field calculation by hitting this “Refresh” icon and perform the re-calculation.
Developer Notes:
Each rollup attribute for an entity has two supporting attributes for the rollup attribute which automatically gets created when a rollup field is created:
- <attribute SchemaName>_Date: DateTime – When the rollup was last calculated
- <attribute SchemaName>_State: Integer – The state of the rollup calculation
Following are the State values:
0 – NotCalculated: Attribute value is yet to be calculated
1 – Calculated: Attribute value has been calculated per the last update time in <attribute SchemaName>_Date attribute
2 – OverflowError: Attribute value calculation lead to overflow error
3 – OtherError: Attribute value calculation failed due to an internal error, next run of calculation job will likely fix it
4 – RetryLimitExceeded: Attribute value calculation failed because the maximum number of retry attempts to calculate the value were exceeded likely due to high number of concurrency and locking conflicts
5 – HierarchicalRecursionLimitReached: Attribute value calculation failed because maximum hierarchy depth limit for calculation was reached
6 – LoopDetected: Attribute value calculation failed because a recursive loop was detected in the hierarchy of the record
Request/Response Messages provided for Rollup Field calculation:
Developers can calculate a rollup attribute value on demand using “CalculateRollupField” message which is supported by Rollup attributes.
Request/Response | Members |
CalculateRollupFieldRequest | Target: EntityReference for the record.FieldName: String representing the logical name of the attribute. |
CalculateRollupFieldResponse | Entity: Entity containing the rollup attribute and the supporting <attribute SchemaName>_Date and <attribute SchemaName>_State attributes. |
Supported Data types:
Rollup field is not supported by all data types and only following data types support rollup field:
- Whole Number
- Decimal
- Currency
- Date & Time
Limitations:
- Rollup works on 1:N relationship. They don’t work on N-N relationship
- Rollup field cannot use another Calculated or Rollup field for rollup
- For Complex field calculations we need to still rely on plug-in or java script
- Rollup fields don’t raise the event to trigger workflows
- We can have maximum of 100 rollup fields within an organization and each entity can have no more than 10 rollup fields.