When it comes to performing calculations on your data, the platform provides a number of options: reports, the Analytics API, dashboards, Apex Triggers, Visualforce, and Rollup Summaries. The Rollup summary option provides a code-free solution with the ability to apply some condition filtering. Rollup summary fields can then also be referenced and combined with other features such as formula fields and Validation Rules.
The key requirement is that there is a Master-Detail relationship between the detail records being summarized on the field being added to the master records, such as that between Race and Contestant. The following example will total the number of competitors that did not finish a given race (or DNF in Formula 1 terms).
Perform the following steps in the package org to try out this process:
- Create a DNF field on the Contestant object using the Checkbox field type.
- Create a Driver record for Charles Pic and add him to the Spa race via the Contestant object, checking the DNF field.
- Create a Total DNFs field on the Race object using a Rollup Summary field type. Select the Contestants object from the Summarized Object dropdown and COUNT as Rollup-Up Type and apply the Filter Criteria field, as shown in the following screenshot, to select only Contestant records with the DNF field checked:
Navigate to the Spa record and note that Total DNFs shows the value 1. Experiment by changing the status of the DNF field on the Driver records to see the value recalculate.
Rollup summaries provide a real-time calculation once configured. However, when creating or changing rollups, Salesforce states that it can take up to 30 minutes. You can read more about them by searching for About Roll-Up Summary Fields in the documentation and by reviewing the Roll-Up Summary Field technology overview knowledge base article at http://help.salesforce.com/apex/HTViewSolution?id=000004281.
As you can imagine, calculating Rollup summaries can be quite an intensive job for the Salesforce servers. As such, Salesforce limits the total number of Rollup summaries to 25 per object, though this can be increased to 40 by contacting Salesforce Support with a valid business case. Keep in mind that, as a packaged application provider, this limit is not scoped by your namespace, unlike other limits. This means that if you use all 10, your end users will not be able to add their own post-installation. Similarly, if they have created their own post-installation and an upgrade causes the limit to be breached, the install will fail.