LearnFrenzy
Sign in Take a free test

Apex Triggers · Lesson 27 of 41

Salesforce Order of Execution

5 min read Updated Nov 8, 2026 Saurabh Samir

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

  1. Load and validate inputoriginal record loaded; new values applied; from the UI, system validation (required fields, formats, lengths)
  2. Before-save record-triggered flowsfast field updates
  3. Before triggersApex before insert / before update
  4. System and custom validationrequired fields again, then your validation rules
  5. Duplicate rulesblock or alert
  6. Save (not committed)record written; Id assigned on insert
  7. After triggersApex after insert / after update
  8. Assignment, auto-response rulesLeads and Cases
  9. Workflow ruleslegacy; a field update re-runs before and after update triggers once more
  10. Escalation rules, processeslegacy Process Builder automation
  11. After-save record-triggered flowsactions and related record updates
  12. Entitlement rulesCases
  13. Roll-up summariesparent (and grandparent) records recalculated – they go through their own save
  14. Criteria-based sharingsharing recalculated
  15. Commiteverything written permanently
  16. 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);

Debug log

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

SituationConsequence
A before-save flow and a before trigger set the same fieldthe trigger runs later, so its value wins
A trigger sets a field a validation rule checksthe rule sees the trigger's value (validation runs after before triggers)
You need the record Id or auto-numberuse an after trigger or after-save flow
A workflow field update or roll-up touches the recordtriggers can run again in the same transaction – make them idempotent
You send an email or enqueue a jobit happens only after commit, and not at all if the save fails
Several triggers on the same objecttheir 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

  1. A validation rule requires Region__c to 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.

  2. 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.

Next lesson

Search LearnFrenzy

Esc

Quick links