# Order of Execution

> What happens, step by step, when a record is saved — and why it matters for triggers, flows and validation rules.

**Track:** developer · **Category:** Apex · **Topics:** Order of execution, Triggers, Flow, Validation rules

Read on the site: https://www.forcechallenges.com/notes/order-of-execution

Knowing the save order lets you answer *"why didn't my validation rule see the value my trigger set?"* style questions.

## The sequence (simplified)

1. **Load** the original record (update) or initialize new values (insert).
2. **System validation** — required fields, field formats, max length (and layout rules from the UI).
3. **Before-save record-triggered flows** (fast field updates).
4. **Before triggers** (`before insert` / `before update` / `before delete`).
5. **System validation again** + **custom validation rules**.
6. **Duplicate rules**.
7. Record **saved to the database** (not committed yet).
8. **After triggers**.
9. **Assignment rules** → **auto-response rules**.
10. **Workflow rules** — field updates here re-run **before/after update triggers once more**.
11. **Escalation rules**.
12. **After-save record-triggered flows** (and other flow automation).
13. **Entitlement rules**.
14. **Roll-up summary** recalculation on the parent (the parent goes through its own save).
15. **Criteria-based sharing** evaluation.
16. **Commit** to the database.
17. **Post-commit**: emails sent, async Apex (`@future`, queueable) and outbound messages start.

## Consequences you should be able to explain

- A **before-save flow** runs **before** your before trigger — your trigger sees the flow's changes.
- **Validation rules run after before triggers** — so values set in a before trigger are validated.
- An **after trigger** that updates the same object starts a **new save cycle** for those records (recursion risk).
- A **workflow field update** can make your update triggers fire **twice** in one transaction.
- **Async work and emails** only happen after commit — if the transaction rolls back, they never run.

## Example

```apex
trigger LeadTrigger on Lead (before insert) {
    for (Lead l : Trigger.new) {
        if (String.isBlank(l.LeadSource)) l.LeadSource = 'Web';
    }
}
```

A validation rule `ISBLANK(LeadSource)` will **not** fire for these leads, because validation rules run after the before trigger filled the field.

## Interview questions

1. *Do validation rules run before or after before-triggers?* → After.
2. *Why might an update trigger run twice?* → Workflow field update re-fires it (or recursion from an after trigger).
3. *When are emails from a trigger actually sent?* → After commit.

---
Source: ForceChallenges · last updated 2026-10-03
