Sub topic 14
Order of execution: what runs when a record is saved
This chapter assumes you know objects, fields and records, and have seen a validation rule, a record-triggered flow and an Apex trigger. It explains the sequence Salesforce follows on every save, so you can predict how several automations interact.
Why the order matters
When a user, an API call or an import saves a record, Salesforce does not run all automation at once. It runs a fixed sequence. If two tools change the same field, the one that runs later wins. If a validation rule reads a field that automation changes, the result depends on when each step runs.
The simplified sequence
The full order has many steps. For tool selection, this shortened version covers what you need.
- Load the record and apply the new field values from the request.
- Run system validation, such as required fields and field formats.
- Run before-save record-triggered flows.
- Run before Apex triggers.
- Run most custom validation, including validation rules.
- Save the record to the database, but do not commit it yet.
- Run after Apex triggers.
- Run legacy workflow rules. A workflow field update can cause before and after triggers to run once more.
- Run after-save record-triggered flows (and any remaining Process Builder processes).
- Run roll-up summary field calculations on parent records.
- Commit the transaction, then run post-commit work such as emails and queued asynchronous jobs.
Check the current "Triggers and Order of Execution" page in the Salesforce developer documentation. The exact steps change between releases.
What this means in practice
- Before-save changes need no extra DML. Before-save flows and before triggers edit the record in memory. Validation rules then see the changed values.
- After-save automation sees saved data. The record has an Id and can be related to other records. A change to the same record at this stage costs another update, which can run the automation again.
- An error rolls everything back. If any step throws an unhandled error, the whole transaction is undone, including earlier steps.
- Asynchronous work runs later. A Queueable job starts in its own transaction with its own limits, after the commit.
Common errors
- Assuming flows run before triggers in every case. Only before-save flows run before before-triggers. After-save flows run after after-triggers.
- Two tools setting the same field. A before-save flow sets
Ratingto "Warm", then a before trigger sets it to "Hot". The trigger wins. Pick one owner for each field. - Recursion. An after-save flow that updates its own record can fire itself again. Add entry conditions, or use a before-save flow when you only change the triggering record.
- Relying on order between triggers. Salesforce does not guarantee the order of multiple triggers on one object. Use one trigger per object.
Exercise
A before-save flow sets Account Rating to "Warm". A before Apex trigger on the same object sets it to "Hot". A validation rule rejects any account with Rating "Hot" and no phone number. What value does the validation rule see, and why?
Show answer
It sees "Hot". The before-save flow runs first, then the before trigger overwrites the value, and validation rules run after both. An account without a phone number is rejected, and the transaction rolls back.