Sub topic 12
Summarizing only the records you want
This chapter assumes you can create a master-detail relationship and a basic roll-up summary field. It covers filter criteria, which limit the child records a roll-up counts.
Why filter a roll-up
By default, a roll-up summary field includes every child record. That is often wrong. An invoice total should skip non-billable lines. An account's open case count should skip closed cases.
When you create the roll-up, the wizard asks which records to include:
- All records should be included in the calculation. This is the default.
- Only records meeting certain criteria should be included. This shows filter rows.
How filter rows work
Each filter row has three parts: a field on the detail object, an operator such as EQUALS or GREATER THAN, and a value. When you add several rows, a child record must meet all of them to be included.
The filter reads fields on the child (detail) object only. It cannot read fields on the master record.
Worked example
Suppose Line_Item__c is the detail object of Invoice__c. You want Total_Amount__c on the invoice to add up only billable lines. These are the settings:
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 TrueSay an invoice has three lines:
| Line | Amount__c | Is_Billable__c |
|---|---|---|
| 1 | 500 | True |
| 2 | 200 | False |
| 3 | 300 | True |
Total_Amount__c is 800. Line 2 is ignored.
What happens when a record changes
Salesforce re-evaluates the filter whenever a child record is created, edited, or deleted. A record can therefore move in or out of the total:
- If you set
Is_Billable__con line 2 to True, the total becomes 1,000. - If you uncheck it on line 1, the total becomes 300.
Common errors
- Blank filter values. A row such as
Is_Billable__c EQUALS Falsedoes not match records where the checkbox is blank in a way you might expect from other tools. Test with real records. - Wrong field type for the operation. SUM, MIN, and MAX need a number, currency, or date field to summarize. COUNT needs no field.
- Expecting OR logic. Rows combine with AND. For an OR condition, create a helper checkbox on the child object, such as a formula field that returns True when either condition holds. Then filter on that checkbox.
Exercise
A Project_c master has Taskc detail records. You want a COUNT roll-up that counts only tasks where Statusc equals "Open" and Priority_c equals "High". How many filter rows do you need, and which records are counted?
Show answer
Two filter rows: Task_c.Statusc EQUALS Open, and Taskc.Priority_c EQUALS High. Rows combine with AND, so only tasks that are both Open and High priority are counted.