Apex Triggers · Lesson 27 of 41
Salesforce Order of Execution
When a record is saved, Salesforce runs a long, fixed sequence: flows, triggers, validation, duplicate rules, more automation, roll-ups, sharing, then commit. Knowing this sequence answers questions like "why didn't my validation rule see the value my trigger set?" and "why did my trigger run twice?" – and it's one of the most common interview topics for Salesforce developers.
In this lesson
- The order of execution, step by step
- Seeing before and after triggers in a real log
- Why triggers can run twice
- Practical consequences for your design
The sequence
What happens when a record is saved
- Load and validate inputoriginal record loaded; new values applied; from the UI, system validation (required fields, formats, lengths)
- Before-save record-triggered flowsfast field updates
- Before triggersApex before insert / before update
- System and custom validationrequired fields again, then your validation rules
- Duplicate rulesblock or alert
- Save (not committed)record written; Id assigned on insert
- After triggersApex after insert / after update
- Assignment, auto-response rulesLeads and Cases
- Workflow ruleslegacy; a field update re-runs before and after update triggers once more
- Escalation rules, processeslegacy Process Builder automation
- After-save record-triggered flowsactions and related record updates
- Entitlement rulesCases
- Roll-up summariesparent (and grandparent) records recalculated – they go through their own save
- Criteria-based sharingsharing recalculated
- Commiteverything written permanently
- After commitemails sent, async Apex and flow asynchronous paths start
Workflow rules and Process Builder are retired for new automation, but many orgs still contain them, so you'll meet them in existing systems.
Seeing it in a log
// CaseTrigger.trigger
trigger CaseTrigger on Case (before insert, after insert, before update, after update) {
for (Case c : Trigger.new) {
String stage = Trigger.operationType.name();
if (Trigger.isBefore && Trigger.isInsert && c.Priority == null) {
c.Priority = 'Medium'; // before: change the record directly
}
System.debug(stage + ' | Id ' + (c.Id == null ? 'not yet assigned' : 'assigned')
+ ' | Priority ' + c.Priority + ' | CreatedDate ' + (c.CreatedDate == null ? 'empty' : 'set'));
}
// after insert: an update of the SAME records starts the save sequence again
if (Trigger.isAfter && Trigger.isInsert) {
List<Case> again = new List<Case>();
for (Case c : Trigger.new) again.add(new Case(Id = c.Id, Subject = c.Subject + ' [routed]'));
update again;
}
}
insert new Case(Subject = 'Printer offline', Priority = null);
System.debug('Final subject: ' + [SELECT Subject FROM Case LIMIT 1].Subject);
BEFORE_INSERT | Id not yet assigned | Priority Medium | CreatedDate empty AFTER_INSERT | Id assigned | Priority Medium | CreatedDate set BEFORE_UPDATE | Id assigned | Priority Medium | CreatedDate set AFTER_UPDATE | Id assigned | Priority Medium | CreatedDate set Final subject: Printer offline [routed]
The log shows the sequence in action: the before trigger runs with no Id and no CreatedDate and sets the priority; after insert sees the saved values; and because the after trigger updated the same record, the whole sequence started again for an update – before update, after update – inside the same transaction. That's the classic cause of "my trigger runs twice", and why the previous lesson's recursion guard matters.
What this means for your design
| Situation | Consequence |
|---|---|
| A before-save flow and a before trigger set the same field | the trigger runs later, so its value wins |
| A trigger sets a field a validation rule checks | the rule sees the trigger's value (validation runs after before triggers) |
| You need the record Id or auto-number | use an after trigger or after-save flow |
| A workflow field update or roll-up touches the record | triggers can run again in the same transaction – make them idempotent |
| You send an email or enqueue a job | it happens only after commit, and not at all if the save fails |
| Several triggers on the same object | their order isn't guaranteed – use one trigger per object |
Flow or trigger for the same object?
Mixing many record-triggered flows and Apex triggers on one object makes the order hard to reason about. Pick a primary tool per object; flows can define their trigger order among themselves (Flow Trigger Explorer), but not relative to Apex triggers beyond the fixed before/after positions above.
Practice
Try it yourself
A validation rule requires
Region__cto be filled. A before insert trigger fills it automatically. Will records created without a region pass validation?Show answer
Yes. Custom validation rules run after before triggers, so they see the value the trigger set.
A roll-up summary on Account sums Opportunity amounts, and an Account trigger runs whenever an Opportunity is saved. Why?
Show answer
Roll-up recalculation saves the parent Account, which runs the Account's own save sequence – including its triggers.
Quick check
Which runs first: a before-save flow or a before trigger?
The before-save flow.
Do validation rules run before or after before triggers?
After.
When are emails from a transaction sent?
After the commit.
Summary
- Before-save flows → before triggers → validation → duplicate rules → save → after triggers → other automation → after-save flows → roll-ups → sharing → commit → after commit.
- Validation rules see values set by before triggers.
- Updates to the same record and roll-ups re-run the sequence – keep triggers idempotent.
- Emails and async jobs start only after a successful commit.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.