📖 Read · 20 XP
Formula fields and roll-up summaries
Formula fields and roll-up summaries
Before writing Apex to calculate something, check whether a formula can do it.
Formula fields
A formula is a read-only field calculated on the fly whenever a record is read. Nothing is stored, so it's never stale.
// Days until a quest is due
Due_Date__c - TODAY()
// Badge text
IF(XP__c >= 500, "🐉 Legendary", IF(XP__c >= 100, "⚔️ Epic", "🗡️ Common"))
// Cross-object (child → parent): reach up through relationships
Account.Owner.Email
Handy functions: IF, CASE, ISBLANK, TEXT (picklist to string), ISPICKVAL, TODAY, NOW, BLANKVALUE, HYPERLINK, IMAGE.
Limits to remember: a formula can't reference itself, can't look down to child records, and has a compiled size limit (5,000 characters).
Roll-up summary fields
Calculated on the master of a master-detail relationship, over its detail records:
| Function | Example |
|---|---|
| COUNT | Number of Enrollments on a Course |
| SUM | Total amount of an Opportunity's line items |
| MIN / MAX | Earliest / latest due date |
You can add filter criteria, e.g. count only Enrollments where Status = 'Active'.
Lookups don't get roll-ups. For lookup relationships you need a Flow, Apex, or the free DLRS (Declarative Lookup Rollup Summaries) tool.
🦝 A formula field saved Quill from writing a trigger once. He's been grateful ever since.