Sub topic 3
When validation rules, formulas and roll-ups are enough
This chapter assumes you know objects, fields, records and relationships, and that you can find your way around Setup and the Object Manager. Before you build a flow or write a trigger, check whether one of three declarative features already solves the problem. They need no flow logic and no deployment of code, and they are the simplest tools to maintain.
The three tools and what each one does
| Tool | What it does | Runs when |
|---|---|---|
| Validation rule | Blocks a save and shows an error message | A record is inserted or updated |
| Formula field | Shows a value calculated from other fields | The record is read |
| Roll-up summary field | Totals child records on the parent | Child records change |
Validation rules
A validation rule is a formula that returns true when the data is wrong. If the formula returns true, Salesforce stops the save and shows your error message. A rule on Opportunity might require a close date once the stage is closed:
AND(
ISPICKVAL(StageName, "Closed Won"),
ISBLANK(CloseDate)
)Validation rules only check data. They cannot change a record or touch other records. If you need to fix a value instead of rejecting it, move on to a flow.
Formula fields
A formula field is a read-only field that computes its value from other fields, including fields on a parent record reached through a lookup or master-detail relationship. Nothing is stored or updated by automation, so the value is always current. For example, a number formula field that shows the discount amount:
Amount * Discount_Percent__c / 100Formula fields cannot be edited, and they cannot be set by a user or a process. They also have limits on formula size and on how many objects they can reference across relationships.
Roll-up summary fields
A roll-up summary field sits on the master record of a master-detail relationship. It calculates a COUNT, SUM, MIN or MAX over the detail records. You can add a filter, such as summing only Opportunities that are closed and won.
Common errors
- Building a flow to display a calculation. If the value only needs to be shown, a formula field is enough. A flow would store a copy that can become stale.
- Writing the validation rule backwards. The formula must be true for invalid data. Many first rules block valid records.
- Expecting a roll-up on a lookup. Roll-up summary fields need a master-detail relationship. For lookups, you need a flow or Apex, or a tool that supports declarative lookup roll-ups.
- Forgetting that validation rules also run on automation. A flow or trigger that saves a record can fail because of a rule, so test those paths.
Exercise
A sales manager wants the number of open Cases shown on each Account, and Case is a lookup to Account, not master-detail. Can a roll-up summary field do this? What would you consider next?
Show answer
No. Roll-up summary fields need a master-detail relationship. Next, consider a record-triggered flow on Case that updates a count field on the Account. For very high volumes or complex logic, an Apex trigger is an option.