Sub topic 1
Two ways to calculate values declaratively
Prerequisites
This chapter assumes you know the following:
- The Salesforce data model: objects, fields, and records.
- How to find your way around Setup and the Object Manager.
- The difference between lookup and master-detail relationships, at a high level.
- Spreadsheet-style logic such as
IFstatements.
Why calculate declaratively
Many values on a record can be derived from other data. Examples are a deal size label, the days until a close date, and the total of an invoice's line items. You can build these with clicks, with no code. Salesforce offers two field types for this: formula fields and roll-up summary fields.
Formula fields
A formula field computes its value from the same record or from related records. It is read-only, and it has a return type such as Text, Number, or Date. Salesforce evaluates the formula each time the value is read, so it always reflects current data.
CloseDate - TODAY()This Number formula on Opportunity returns the days until the close date. The result is negative for past dates.
Roll-up summary fields
A roll-up summary field lives on the master record of a master-detail relationship. It aggregates the child (detail) records into one value. The available summary types are COUNT, SUM, MIN, and MAX. You can add filter criteria so that only some child records are included. Salesforce stores the result on the master record and updates it when child records change.
Comparing the two
| Aspect | Formula field | Roll-up summary field |
|---|---|---|
| Purpose | Derive a value from this record or from related records | Aggregate child records onto a master record |
| Where it lives | Any object | The master object only |
| Relationship needed | None, or any relationship for traversal | Master-detail |
| When it calculates | When the value is read | When child records change |
| Aggregation | Cannot aggregate child records | COUNT, SUM, MIN, MAX |
How they work together
The two types complement each other. A roll-up produces a total or a count. A formula can then use that value in further math, such as an average. The later chapters on combining roll-ups with formulas cover this.
Where this tutorial goes next
The first half covers formulas: return types, operators, IF and CASE, null handling, related-record fields, dates, and limits. The second half covers roll-ups: the master-detail requirement, summary types, filters, recalculation, and limits. The final chapters cover alternatives for lookup relationships and how to choose between a formula, a roll-up, Flow, and Apex.
Exercise
You need the total amount of all line items on each invoice, and the line items have a master-detail relationship to the invoice. Which field type do you use, and on which object does it live?
Show answer
A roll-up summary field of type SUM. It lives on the master object, Invoice, and totals the amount field on the Line Item detail records. A formula field cannot aggregate child records.
Exercise
A formula field and a roll-up summary field both show a calculated number. Which one is calculated when the value is read, and which one is stored and updated when child records change?
Show answer
The formula field is calculated when read. The roll-up summary field is stored on the master record and updated when its child records change.