Sub topic 17
Choosing formula, roll-up, Flow, or Apex
This chapter assumes you can build a basic formula field, know what a roll-up summary field does, and understand the difference between lookup and master-detail relationships. The goal is a repeatable way to pick the right tool for a calculation.
Start with where the data lives
Ask two questions first. Does the value come from the same record, a parent, or child records? Does it need to be stored, or can it be computed when read?
- A formula field reads values from the same record or from parents (up to the relationship limits). It is calculated each time it is read and is never written by DML.
- A roll-up summary field aggregates child records into a master record. It requires a master-detail relationship and supports only COUNT, SUM, MIN, and MAX.
- Flow can aggregate or copy values on lookup relationships and can write to ordinary fields.
- Apex handles anything the declarative tools cannot, at the cost of code to write, test, and maintain.
Decision flow
Comparing the options
| Need | Best fit | Why |
|---|---|---|
| Size label from Amount | Formula | Same-record logic, always current |
| Total of billable line items | Roll-up with filter | Master-detail, SUM with criteria |
| Average line item amount | Formula over two roll-ups | Roll-ups have no AVG, so divide SUM by COUNT |
| Total payments on a lookup | Flow or Apex | No native roll-up on lookup |
| Value that must be set in DML or reported as stored data | Flow or Apex | Formulas and roll-ups cannot be written |
Worked example
An invoice has line items in a master-detail relationship. You need the total, the count, and the average.
- Create
Total_Amount__cas a SUM roll-up andLine_Item_Count__cas a COUNT roll-up. - Create a Number formula on the invoice for the average:
IF(Line_Item_Count__c = 0, 0, Total_Amount__c / Line_Item_Count__c)If the relationship were a lookup, the same result would need a record-triggered Flow or an Apex aggregation like the recalculateTotals method that groups Payment__c rows by account.
Common mistakes
- Choosing Apex first. Declarative options need no tests and are easier for admins to maintain.
- Expecting a formula to be searchable or indexable like a stored field. Heavy formula use in filters can slow queries.
- Converting a lookup to master-detail only to get a roll-up. This changes ownership, sharing, and delete behavior, so check those effects first.
- Forgetting that a code insert does not return calculated values. Re-query the record to read them.
Exercise
Opportunities have a lookup to a custom Region_c object. You need each Region_c to show the highest Amount among its opportunities. Which tool do you choose, and why not a roll-up summary field?
Show answer
Use a record-triggered Flow (or Apex if volume or complexity demands it) that finds the maximum Amount and writes it to a regular number field on Region_c. A roll-up summary field would offer MAX, but it needs a master-detail relationship. Opportunity-to-Region_c is a lookup, so the native roll-up is unavailable.