Sub topic 15
When to use flows or Apex instead
This chapter assumes you can write a basic error condition formula and know the order of execution at a high level. It covers where validation rules stop working and which tool to use next.
What validation rules cannot do
A validation rule is a single formula that reads the record being saved. That design makes it fast and easy to maintain, but it limits what the rule can check:
- It cannot query child records, such as "this Opportunity has no products".
- It can follow lookups to parent records, but it cannot reach unrelated objects.
- It cannot change data, send messages, or call external systems.
- It has a compiled size limit (5,000 characters), so long rules become hard to read before they hit it.
- It cannot ask different questions in different situations without growing into a nest of IF statements.
Choosing a tool
| Requirement | Best fit |
|---|---|
| Compare fields on the record or its parents | Validation rule |
| Check child records or unrelated records | Flow or Apex |
| Block the save and also set other fields | Before-save flow or Apex |
| Complex logic, bulk-safe queries, callouts | Apex |
| Admin-maintainable logic with several branches | Flow |
Start with a validation rule. Move to a flow when the logic needs related data or several steps. Move to Apex when the logic needs queries, loops over many records, or code-level control.
Flows
A record-triggered flow can use a Get Records element to read child or unrelated records. It can then stop the save with the Custom Error element. A flow is a good choice when an admin team will maintain the logic and the check needs a lookup the formula cannot do.
Remember the order of execution. Before-save flows run before validation rules, so a rule sees the values the flow set. Validation rules do not see changes made by after-save automation.
Apex
An Apex before trigger can run queries and call addError on a record or a field. This trigger blocks a deal from reaching Closed Won without products. A validation rule cannot do that, because it cannot see child records.
trigger OpportunityRequireProducts on Opportunity (before update) {
Set<Id> closing = new Set<Id>();
for (Opportunity opp : Trigger.new) {
if (opp.StageName == 'Closed Won'
&& Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {
closing.add(opp.Id);
}
}
if (closing.isEmpty()) {
return;
}
Set<Id> withLines = new Set<Id>();
for (AggregateResult ar : [
SELECT OpportunityId oppId
FROM OpportunityLineItem
WHERE OpportunityId IN :closing
GROUP BY OpportunityId
]) {
withLines.add((Id) ar.get('oppId'));
}
for (Id oppId : closing) {
if (!withLines.contains(oppId)) {
Trigger.newMap.get(oppId).addError(
'Add at least one product before closing the deal.'
);
}
}
}The trigger runs one query for the whole batch, so it stays within governor limits during data loads.
Trade-offs
- Validation rules respect your bypass custom permission only if you write the clause. Flows and Apex need their own bypass check, such as
FeatureManagement.checkPermission('Bypass_Validation_Rules')in Apex. - Logic split across rules, flows, and triggers is harder to debug. Document where each requirement lives.
- Apex needs tests and a deployment process. A flow or rule does not need code tests, but you should still test it.
Exercise
A rule must block an Account from being marked Inactive while it still has open Cases. Can a validation rule do this? What would you use instead?
Show answer
No. A validation rule cannot query child records such as Cases. Use a before-save record-triggered flow that looks up open Cases and raises a Custom Error, or an Apex before trigger that queries Cases and calls addError. The flow is the better first choice if admins will maintain it.