Sub topic 13
Where validation rules sit in the order of execution
This chapter assumes you can write a basic validation rule formula and read simple Apex triggers. You do not need to write Apex from scratch, but you should know what before insert and after update mean.
A save in Salesforce is more than "check the rules, then write the record." Triggers, flows, and system checks all run in a fixed sequence, and your validation rule is one step in it. Where it sits decides which values it sees, which automation runs before it, and which changes it never checks.
The save sequence in outline
The full order of execution has many steps. The ones that matter for validation rules are below. The sequence is the same whether the save comes from the UI, the API, Apex DML, or a data load. The only difference is that some UI-specific checks are skipped for other sources.
- Salesforce loads the original record, or initializes a new one for an insert.
- The new field values from the request overwrite the old values.
- System validation runs: required fields on the page layout, field formats, and maximum lengths.
- Before-save record-triggered flows run.
- Apex
beforetriggers run. - System validation runs again, and then your custom validation rules run.
- Duplicate rules run.
- The record is saved to the database, but not yet committed.
- Apex
aftertriggers run. - Assignment rules, auto-response rules, workflow rules, and escalation rules run.
- After-save record-triggered flows run.
- Roll-up summary fields and other cross-object updates are calculated.
- The transaction commits, and post-commit work such as email sending happens.
The exact list changes between Salesforce releases and has more steps than shown here. Check the "Triggers and Order of Execution" page in the Apex Developer Guide for your release. The relative position of the validation step has stayed stable: after before-save automation, before the database write.
What this means for the values a rule sees
Before-save automation runs first
A validation rule evaluates the record as it looks after before-save flows and before triggers have finished. Two consequences follow.
- Automation can fix data before the rule judges it. A before trigger can normalize a value, and the rule then sees the cleaned version.
- Automation can also break data the user entered correctly. If a before trigger overwrites a field, the rule blames the user for a value the user never typed. Error messages should name the field and the requirement, so the cause is easier to find.
Here is a before trigger that strips spaces from a postal code. It lets a ZIP code typed as 94 105 pass the REGEX rule from the earlier chapter.
trigger AccountNormalizePostalCode on Account (before insert, before update) {
for (Account acc : Trigger.new) {
if (acc.BillingPostalCode != null) {
acc.BillingPostalCode = acc.BillingPostalCode.deleteWhitespace();
}
}
}The trigger changes the value in memory in step 5. The validation rule runs in step 6 against the cleaned value. The user never sees the intermediate state.
Changes made after validation are not re-checked
Steps after the validation step can still modify the record. The main cases are:
- Workflow field updates. If a workflow rule updates a field, before and after update triggers fire once more. Custom validation rules do not run again for that update. A workflow field update can therefore leave a record in a state your rule would have rejected.
- After-save flows that update other records. Each affected record goes through its own save, so its own validation rules run. If one fails, the error surfaces in the original transaction and the whole thing rolls back.
- Roll-up summary fields. When a child change alters a roll-up value on the parent, the parent goes through its own save steps, which can trigger the parent's validation rules. This surprises users who edit a child record and get an error about a field on the parent.
What a failure does
If any validation rule evaluates to true, the save for that record stops. Salesforce rolls the record back and returns every failing rule's error message together, not just the first. All active rules on the object are evaluated, and the order among them is not guaranteed. Do not write one rule that assumes another has already passed.
Because the check happens in step 6, work from earlier steps has already happened for the failed record:
- Before triggers ran. A before trigger may have executed for a record that then fails validation. Keep before triggers free of side effects that cannot be undone, such as callouts. Database changes within the transaction are rolled back, but a callout is not.
- After triggers did not run. After triggers fire only for records that were saved. Inside an after trigger you can rely on the record having passed all custom validation rules.
- Partial success changes the scope. With
Database.insert(records, false), the failing records are rejected and the passing records are saved. After triggers then run only for the saved ones. With the default all-or-none behavior, one failing record rolls back the whole batch.
| Where your code runs | Has validation run on this record? | Can it assume the data is valid? |
|---|---|---|
| Before-save flow | No | No |
| Apex before trigger | No | No |
| Apex after trigger | Yes, and it passed | Yes, for custom validation rules |
| Workflow field update | Yes, but not re-run after the update | No |
| After-save flow | Yes, and it passed | Yes, as of the save, but not after its own updates |
Practical decisions that follow from the order
- Do not duplicate validation in before triggers. A trigger that checks the same condition as a rule runs first and can fire its own error. Users then see two sources of truth. Put declarative checks in the rule and keep triggers for logic a formula cannot express.
- Use defaulting automation to prevent false errors. If a field is required only because a before-save flow sets a default, the rule can safely assume the default exists. The rule must not fire on the request value before the flow runs, and it does not, because the flow runs first.
- Beware of cascades. A flow that updates a parent from a child save can surface the parent's rule failures on the child. When you debug "wrong object" errors, look at which automation touches related records.
- Account for bypass checks. Custom permission checks such as
$Permission.Bypass_Validation_Rulesare evaluated at step 6 like the rest of the formula. A bypass skips only the rule it is written into. It does not skip system validation, triggers, or flows. - Read debug logs for the order. In a debug log,
CODE_UNIT_STARTEDlines forValidation:Opportunityappear between the before-trigger and after-trigger units. Seeing this in your own logs is the quickest way to confirm the sequence for an object.
Exercises
Exercise
A before-save flow sets Amount to 100 whenever it is blank on an Opportunity in Negotiation/Review. A validation rule requires Amount to be non-blank in that stage. A user saves an Opportunity in that stage with Amount blank. Does the rule block the save, and why?
Show answer
No. The before-save flow runs before custom validation rules, so Amount is already 100 when the rule evaluates. ISBLANK(Amount) is false, the formula returns false, and the save succeeds.
Exercise
A workflow field update sets Amount to 0 after the save. Your rule rejects Amount <= 0 in Negotiation/Review. Will the rule block the workflow update? What would you do if you needed that guarantee?
Show answer
No. Custom validation rules do not run again after a workflow field update, so the record can end up with Amount 0. To get the guarantee, set the value earlier with a before-save flow or before trigger, where the rule will check it, or remove the field update.