Sub topic 14
Roll-up restrictions and per-object limits
This chapter assumes you can create a roll-up summary field and understand master-detail relationships. If you can't, review the chapters on those first. Here you learn what roll-ups can't do and where Salesforce caps them. You need this before you design a data model, because several limits are expensive to work around later.
Limits at a glance
| Limit or rule | Value | Notes |
|---|---|---|
| Roll-up summary fields per object | 25 | Salesforce Support can raise this to 40. |
| Master-detail relationships per custom object | 2 | Each one can be the base for roll-ups on its master. |
| Levels of master-detail nesting | 3 | Master, detail, sub-detail. |
| Summary types | COUNT, SUM, MIN, MAX | No average, median, or distinct count. |
Check current numbers in the Salesforce documentation for your edition and release, because limits change and editions differ.
Where a roll-up can live
A roll-up summary field exists only on the master object of a master-detail relationship. It cannot exist on:
- The detail side of a master-detail relationship, unless that object is also the master of another relationship further down the chain.
- The parent of a lookup relationship.
- Objects that cannot be the master in a master-detail relationship. Standard objects vary here. Account, Opportunity, and Campaign support roll-ups, but activities (tasks and events) do not.
Lookup relationships are the usual surprise. A team often builds a lookup, then discovers there is no Roll-Up Summary option in the new-field wizard. The next chapters cover the alternatives.
What each summary type can summarize
The field you choose to total depends on the summary type.
| Summary type | Source field types |
|---|---|
| COUNT | None. It counts detail records. |
| SUM | Number, currency, percent |
| MIN and MAX | Number, currency, percent, date, date/time |
Consequences:
- You cannot SUM a text, picklist, or checkbox field. To count checked boxes, use COUNT with filter criteria such as
Is_Billable__c EQUALS True. - You cannot total a date. MIN and MAX let you find the earliest or latest date among the children.
- Salesforce does not provide AVERAGE. Calculate it with a formula that divides a SUM roll-up by a COUNT roll-up, guarding against zero, as shown in the formula chapter on roll-ups.
- There is no text roll-up. You cannot concatenate or list child values.
Formula fields as the source
A roll-up can summarize a formula field on the detail object, with limits. A formula that references fields on other objects, or that depends on a changing value such as TODAY() or NOW(), cannot be used as a roll-up source. The reason is that a roll-up is stored and recalculated only when a child record changes. A value that changes with the calendar or with another record would leave the roll-up out of date without any child edit to trigger recalculation.
Roll-ups of roll-ups
A detail object can itself hold a roll-up from its own children, and the master above it can summarize that value. This works across the three-level chain, but each level adds recalculation work and record locking. Use it deliberately.
Filter criteria restrictions
Filter criteria let a roll-up include only some detail records. They have their own rules:
- Filters apply only to fields on the detail object. You cannot filter on the master's fields or on fields from other related objects.
- Only some field types appear in the filter list. Long text and some system fields are not available.
- A filter is evaluated per detail record against stored values. A cross-object formula on the detail record cannot be used as a filter field.
Schema dependencies that block changes
A roll-up creates a dependency between the master and detail objects. Salesforce protects it in several ways:
- You cannot delete a detail field that a roll-up sums or uses in a filter. Delete or edit the roll-up first.
- You cannot change the data type of a field that a roll-up depends on.
- You cannot convert a master-detail relationship to a lookup while roll-up summary fields exist on the master. Remove them first.
- Converting a lookup to master-detail needs every existing child record to have a parent value. That conversion often fails on old data with blank lookups.
- A formula, validation rule, or report that references a roll-up field stops you from deleting it until you remove the reference.
Performance and locking costs
Roll-ups are stored values, so reads are fast. Writes pay the cost. When you insert, update, or delete a detail record that affects a roll-up, Salesforce recalculates the roll-up on the master and locks the master record while it does. Heavy parallel loading of children that share one parent can produce UNABLE_TO_LOCK_ROW errors. Common mitigations:
- Sort batches by parent in data loads so one parent's children go in the same batch.
- Reduce parallelism, for example with the Bulk API serial mode.
- Avoid having a single "bucket" master that thousands of children point to.
When you reach a limit
- Review roll-ups that overlap. For example, two roll-ups that differ only by filter may be replaceable with one roll-up plus a formula.
- Check whether a value really needs to be stored. A formula on the detail object can sometimes provide what you need without a roll-up.
- Request the higher limit from Salesforce Support if the use case is legitimate.
- Move the calculation to Flow or Apex when you need an average, a text result, a calculation across a lookup relationship, or logic the four summary types can't express.
Exercise
A team has a lookup field from Payment__c to Account and wants a SUM of payment amounts on Account. They also want the average payment and the latest payment date. Which of these can a standard roll-up summary field provide, and why?
Show answer
None of them as built. A roll-up summary field needs a master-detail relationship, and this relationship is a lookup. After converting it to master-detail (possible only if every payment has an account), the SUM and the latest date (MAX on a date or date/time field) are available directly. There is no AVERAGE summary type. Create a SUM roll-up and a COUNT roll-up and divide them in a formula, guarding against a count of 0. If converting is not possible, aggregate with Flow or Apex instead.
Exercise
An admin tries to delete the field LineItemc.Amount_c and gets an error. The field is no longer shown on any page layout. What is the most likely cause, and what is the fix?
Show answer
A roll-up summary field on the master object (for example TotalAmountc) uses Amount_c as its field to total or in its filter criteria. Salesforce blocks deleting a field that a roll-up depends on. Delete or edit the roll-up, and remove any formulas, validation rules, or reports that reference the roll-up. Then delete the detail field.