Sub topic 3
When validation rules, formulas and roll-ups are enough
This chapter assumes you know objects, fields, records and relationships, and can find your way around Setup and the Object Manager. You don't need to read Apex. By the end you should be able to tell when a requirement needs no Flow or Apex at all, and what limits push you to a heavier tool.
Why these three features come first
The clicks-before-code principle says to start with the simplest tool that works. Validation rules, formula fields and roll-up summary fields are the simplest tools on the platform. They need no flow or class to maintain, no test coverage and no activation step. They are also evaluated by the platform itself, so they behave the same whether a record is saved from the UI, the API, an import, Apex or a flow.
Each tool answers a different question:
| Tool | Question it answers | Writes data? |
|---|---|---|
| Validation rule | Is this record allowed to be saved? | No. It blocks the save. |
| Formula field | What value can I calculate from this record and its parents? | No. It is calculated when read. |
| Roll-up summary field | What is the total, count, minimum or maximum of the child records? | Yes. The platform stores and maintains it. |
Validation rules
A validation rule is a formula that returns TRUE when the data is invalid. This is the most common mistake: you write the condition for the error, not for the good case. When the formula is true, the save fails and the user sees your message.
AND(
ISPICKVAL(StageName, "Closed Won"),
OR(ISBLANK(Amount), Amount <= 0)
)Set the error message to something like "Enter an amount greater than zero before closing the opportunity as won." You can show it at the top of the page or next to a specific field. Next to the field is easier for users to act on.
Functions you will use often:
ISNEW()is true only when the record is being created.ISCHANGED(field)is true when the field value differs from the saved value.PRIORVALUE(field)returns the saved value, so you can block specific transitions.ISBLANK()handles null and empty text. Prefer it over comparing to"".ISPICKVAL()is the only safe way to test a picklist value.
A rule that blocks backward stage movement for non-admins looks like this:
AND(
ISCHANGED(StageName),
$Profile.Name <> "System Administrator",
ISPICKVAL(PRIORVALUE(StageName), "Closed Won")
)Gotchas with validation rules
- They run on every save path. A data load, an integration or a flow that updates the record will hit the rule too. A rule that suits a user typing in the UI can break a nightly integration.
- Plan a bypass. A custom permission lets a trusted integration user skip the rule. Add
NOT($Permission.Bypass_Validation)to the formula withAND. This is cleaner than checking a profile name. - They can't change data. If the requirement is "fix the value automatically", a validation rule is the wrong tool. Use a before-save flow instead.
- They only see what the formula can reach. A rule can read the record and fields on its parent records. It can't query child records. "Block closing if there are open tasks" can't be done with a plain validation rule. A roll-up summary field that counts open tasks can feed the rule, though, when the relationship allows it.
Formula fields
A formula field calculates its value when it is read. Nothing is stored in the usual sense, so the value can never go stale. It also can't be edited, and it adds no save-time logic.
IF(
AnnualRevenue >= 1000000,
"Enterprise",
IF(AnnualRevenue >= 100000, "Mid-market", "SMB")
)Formula fields are good for:
- Labels derived from other fields, as above.
- Values pulled from a parent, such as
Account.Industryshown on a case. - Date arithmetic, such as
CloseDate - TODAY()for days to close. - Visual cues, such as a status indicator built with
IMAGE()or an emoji in text.
Limits to know:
- A formula has a compiled size limit (5,000 characters). Large formulas must be split into helper formula fields.
- A formula can reference fields across up to 10 unique object relationships.
- Formulas can't reference child records. Going down the hierarchy needs a roll-up summary field or automation.
- Because the value is computed, it is awkward to filter and report on at very large data volumes. Check whether the formula is indexable if it appears in a list view or report filter on a big object.
If you need the value to be set once and stay fixed, such as "the rating at the time of closing", a formula is wrong. A formula always reflects current data. Use a stored field set by a before-save flow.
Roll-up summary fields
A roll-up summary field aggregates child records onto their parent. It supports four operations: COUNT, SUM, MIN and MAX. You can add filter criteria, such as "sum only opportunities where IsWon is true".
There is one big restriction. A roll-up summary field can only be created on the master object in a master-detail relationship. It does not work on a lookup relationship. This is the most common reason a "just use a roll-up" suggestion fails.
Other limits:
- There is a per-object limit on roll-up summary fields (25 by default).
- The platform recalculates the parent when a child is inserted, updated, deleted or undeleted, so it keeps the totals correct without any code.
- Roll-ups can't calculate averages directly. Create a
SUMand aCOUNTroll-up, then divide them in a formula field.
IF(Total_Orders__c > 0, Total_Revenue__c / Total_Orders__c, 0)For a lookup relationship, you need a record-triggered flow, Apex, or a managed or open-source roll-up tool. At that point you have left the declarative basics, and the later chapters on flows and triggers apply.
A quick decision path
Where the order of execution fits
Validation rules don't run first. In the save sequence, system validation comes first, then before-save flows and before triggers run, and only then do custom validation rules run. A before-save flow can therefore fix a value before your rule checks it. Formula fields are recalculated after the save, and roll-up summary fields on the parent are updated after the child save completes. A later chapter covers the full sequence.
Moving past the basics
Choose a heavier tool when the requirement needs any of these:
- Updating a field, creating a record or sending an email. Validation rules and formulas can't do these.
- Aggregating over a lookup relationship.
- Storing a value that must not change as source data changes.
- Logic that depends on child records, other than a roll-up count.
Otherwise, keep it declarative. Fewer moving parts means fewer conflicts in the order of execution.
Exercise
A sales manager wants to stop users from saving an Opportunity with Stage "Negotiation" unless the Description field has text. What validation rule formula expresses this, and what is the most common mistake in writing it?
Show answer
Use AND(ISPICKVAL(StageName, "Negotiation"), ISBLANK(Description)). The formula must be true when the data is invalid. The common mistake is writing the condition for the valid case, which blocks every other save instead. Also use ISPICKVAL rather than comparing StageName to text.
Exercise
You need the total value of all Invoice_c records on each Customer_c record. The relationship between them is a lookup. Can you use a roll-up summary field? If not, what are your options?
Show answer
No. Roll-up summary fields exist only on the master side of a master-detail relationship. Options are to convert the lookup to master-detail, if the data and sharing model allow it, or to use a record-triggered flow, an Apex trigger, or a roll-up tool to maintain a number field on the parent.
Exercise
A requirement says "Set Rating to Hot when Annual Revenue is at least 1,000,000, and keep it Hot even if revenue later drops." Why is a formula field the wrong choice?
Show answer
A formula field always reflects current data, so the value would change when revenue drops, and a formula can't be edited. The requirement needs a stored value set once, which a before-save record-triggered flow or a trigger can do.