Sub topic 12
Summarizing only the records you want
This chapter covers roll-up filter criteria. It assumes you can create a master-detail relationship and a basic roll-up summary field. You should also be comfortable reading simple formula syntax.
What filter criteria do
A roll-up summary field aggregates every child record by default. A COUNT roll-up on Invoice__c counts every Line_Item__c. A SUM roll-up adds up every Amount__c. Filter criteria narrow the set of child records that the roll-up includes.
Filter criteria apply only to the detail (child) records. The master record is never filtered. If no child record meets the criteria, the roll-up still exists. COUNT returns 0, and SUM returns 0. MIN and MAX return blank.
Use filters when only part of the child data is meaningful for a total. Examples include billable line items, open cases, or closed-won opportunities.
The wizard choices
The roll-up summary wizard asks for the filter after you pick the summary type and the field to total. It offers two options:
- Include all records in the roll-up. This is the default. There is no filter.
- Only records meeting certain criteria should be included in the roll-up. This reveals a filter builder.
Each filter row has three parts: a field on the detail object, an operator, and a value. You can add up to 10 rows. By default the rows combine with AND. Salesforce also lets you write filter logic with AND and OR, such as 1 AND (2 OR 3), where the numbers refer to the row numbers.
A worked example
An invoice has line items. Some are billable and some are not. The invoice total should include only the billable ones. The settings below produce that result.
Master object: Invoice__c
Detail object: Line_Item__c (master-detail field Invoice__c)
Roll-up field: Total_Amount__c on Invoice__c
Summary type: SUM
Field to total: Line_Item__c.Amount__c
Filter criteria: Only records meeting criteria: Line_Item__c.Is_Billable__c EQUALS TrueSuppose an invoice has three line items:
| Line item | Amount | Is_Billable__c |
|---|---|---|
| Consulting | 500 | True |
| Travel | 120 | False |
| Support | 300 | True |
Total_Amount__c is 800. The travel line is excluded because it fails the filter. If someone later changes Travel to billable, the roll-up recalculates to 920.
How filtering is evaluated
Salesforce evaluates the filter on the child record's values. A change to a filtered field can move a record into or out of the roll-up. For example, setting Is_Billable__c to False removes that line from the total. This also means edits to fields that appear in the filter trigger a recalculation of the master.
Operators and value types
The operators available depend on the field type on the detail object.
| Field type | Typical operators |
|---|---|
| Checkbox | equals, not equal to |
| Text | equals, not equal to, starts with, contains, does not contain |
| Number, currency | equals, not equal to, less than, greater than, and the "or equal to" forms |
| Date | equals, not equal to, less than, greater than |
| Picklist | equals, not equal to, and the text-style operators on the label |
Some behaviors need attention:
- A text comparison on a blank field can behave differently from what you expect. If you want to include blanks, add an explicit row such as
Status__c equalswith an empty value, joined with OR. - For multiple values, a comma-separated list works with "equals".
Status__c equals Open,Pendingmatches either value. - Date criteria compare against fixed dates, or against special values such as TODAY or THIS MONTH where the wizard offers them. Prefer fixed field values when you need predictable totals.
Filtering on a formula field
A filter can use a formula field on the detail object. This lets you express logic the filter builder cannot, such as comparing two fields on the child. For example, a checkbox formula Amount__c > Budget__c can serve as the filter field, and the filter would be Is_Over_Budget__c EQUALS True.
Two cautions apply:
- A formula that uses a time-dependent function such as
TODAY()orNOW()does not cause the roll-up to recalculate when only the date changes. Salesforce recalculates the roll-up when a child record is saved, not when a day passes. The result can go stale. - A formula that references other objects, or that is complex, can make the roll-up hard to reason about. Keep the filter formula simple and test it directly.
Check the current Salesforce documentation for exact restrictions on which formula fields are allowed in filters, because they change between releases.
Editing filters after creation
You can change filter criteria on an existing roll-up. Salesforce then recalculates the field for existing records. For large volumes this takes time and can lock master records while it runs. The roll-up shows its values only after the recalculation finishes. Plan changes outside of heavy data-load windows. Chapter content on recalculation and record locking covers the mechanism.
You can also use the Force a mass recalculation of this field option on the field's detail page when data was loaded in a way that left the values wrong.
Common errors and gotchas
- Total looks too high. Check that you actually selected "Only records meeting certain criteria." Leaving the default includes every child.
- Total looks too low. Look for blank values in the filter field. A row such as
Is_Billable__c EQUALS Trueexcludes records where the checkbox is False, which is correct, but a text status filter can silently exclude records with a blank status. - OR logic missing. If you add two rows and expect either to match, you need filter logic. Without it, both rows must be true.
- Roll-up does not change after a date passes. This comes from time-based filters, as described earlier. Use a scheduled Flow or Apex job when the result depends on elapsed time.
- Cannot edit the field you filter on. You cannot delete or change the type of a field used in a roll-up filter until you remove it from the filter.
Verifying the result in code
Roll-up values are read-only in DML, so test them by re-querying the master. This is the same approach used for any calculated field.
Invoice__c inv = new Invoice__c(Name = 'INV-001');
insert inv;
Invoice__c saved = [SELECT Id, Total_Amount__c FROM Invoice__c WHERE Id = :inv.Id];
System.debug(saved.Total_Amount__c);After you insert line items, run the same query again to see the filtered total. The in-memory inv variable never updates by itself.
Using a filtered roll-up in a formula
A filtered roll-up can feed a formula on the master. Pair a filtered SUM with a filtered COUNT, and the average over the same filtered set follows. Guard against division by zero.
IF(Line_Item_Count__c = 0, 0, Total_Amount__c / Line_Item_Count__c)Both roll-ups must use the same filter, or the average will mix different sets of records.
Exercise
An Invoice has three LineItemc children with amounts 200 (IsBillable_c True), 150 (False), and 50 (True). A SUM roll-up has the filter IsBillable__c EQUALS True. What is the total, and what happens when the 150 line is changed to billable?
Show answer
The total is 250, because only the 200 and 50 lines meet the filter. When the 150 line becomes billable, the roll-up recalculates and the total becomes 400.
Exercise
You want a COUNT roll-up that counts cases whose Status is either Open or Escalated, or whose Priority is High. What do you need in the filter beyond adding the three rows?
Show answer
You need custom filter logic so the rows combine as OR instead of the default AND, for example 1 OR 2 OR 3, with a row for each condition.