Order of Execution
By the end of this lesson, you'll be able to:
- Name the fuller order-of-execution sequence, including validation rules and triggers together
- Explain where triggers fit relative to validation rules and other automation
- Connect this back to Module 19's simplified insert sequence
Prerequisites: "Trigger Recursion"
Revisiting Module 19's sequence
Module 19's "What Really Happens When You Insert a Record" lesson introduced a simplified 6-step sequence: system validation, before triggers, custom validation rules, the save, after triggers, other automation. Everything in this module — before/after timing, Trigger.new, handler classes — has been building toward making that sequence concrete rather than abstract.
The order, with real trigger behavior in place
- Existing record loaded from the database (for
update), or a new one initialized (forinsert). - System validation — required fields, data types.
beforetriggers run — able to freely modifyTrigger.new's fields, since nothing is saved yet.- Custom validation rules run — checked against the (possibly
before-trigger-modified) values. - The record is saved —
Idis now assigned for a new record. aftertriggers run — can see the final saved state, includingId, but changing a field here needs a freshupdate.- Other automation — assignment rules, workflow rules, processes.
Steps 3 and 6 are exactly this module's before/after trigger timing distinction, now placed in the full sequence alongside validation rules.
Why validation rules run after before triggers
trigger AccountTrigger on Account (before insert) {
for (Account acc : Trigger.new) {
if (String.isBlank(acc.Industry)) {
acc.Industry = 'Unknown';
}
}
}
Because before triggers (step 3) run before validation rules (step 4), a validation rule requiring Industry to be populated would actually pass for a record that started with a blank Industry — the trigger's default already filled it in by the time validation checks it. This exact ordering is why before triggers are a common, deliberate place to satisfy a validation rule automatically rather than forcing every caller to remember to set the field themselves.
Exercise
As a comment, explain why a before trigger that defaults a blank Industry field would let a validation rule requiring Industry to be non-blank still pass.
Show hint
Think about which of the two steps runs first.
Order of Execution Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson expands Module 19's simplified insert sequence into the fuller order of execution, now that triggers, handler classes, and recursion are all in place to make each stage concrete.