Sub topic 12
After-save flows
This chapter assumes you know how before and after triggers differ and have built a simple record-triggered flow. It covers where after-save flows run, what they can do, and how they interact with the rest of the transaction.
Where after-save flows run
A record-triggered flow is set to run either before the record is saved (fast field updates) or after the record is saved (actions on related records). This chapter covers the second type. It runs after the record has been written to the database but before the commit. That puts it in the middle of the order of execution:
- After triggers run.
- Assignment, auto-response, workflow, escalation, and entitlement rules run.
- After-save record-triggered flows run. Salesforce does not guarantee the order within this group, which also includes legacy Process Builder processes and flows launched by workflow rules.
- Roll-up summary fields, parent saves, and criteria-based sharing are evaluated.
- The transaction commits, and post-commit logic runs.
What the record looks like to the flow
The record already exists in the database, so $Record.Id is populated on insert. This is the main reason to choose an after-save flow over a before-save flow. A flow that creates a child Task or Contact needs the parent's Id.
Fields calculated during the save are also visible, such as a value set by a before trigger or a before-save flow. Formula fields that depend on those values are recalculated when you read them.
You cannot change the triggering record by assigning to $Record as you can in a before-save flow. To change it, you must use an Update Records element. That element performs real DML.
Updating the triggering record
An Update Records element on the triggering record causes a second save of that record. Salesforce treats it like any other DML statement:
- Before and after update triggers run again for that record.
- Validation rules run again.
- Record-triggered flows on that object are evaluated again.
- All of this happens in the same transaction, so it uses the same governor limits.
This is the most common source of unexpected recursion in flow-heavy orgs. If a flow only sets fields on the triggering record, move it to a before-save flow. That avoids the second save.
Entry conditions and re-entry
A record-triggered flow has two settings for update-based runs:
| Setting | Behavior |
|---|---|
| Only when a record is updated to meet the condition requirements | Runs when the record did not meet the criteria before and meets them now. |
| Every time a record is updated and meets the condition requirements | Runs on every qualifying update, including nested saves caused by other automation. |
Choose the first option unless you need the flow to run every time. It is the cheapest protection against loops, because the second save no longer counts as a transition into the criteria.
Bulk behavior
When a data load saves 200 records, Salesforce groups the flow interviews so that elements such as Create Records, Update Records, and Get Records run once per batch. This works only if you keep those elements out of loops. A Get Records or DML element inside a Loop element runs once per iteration and can exceed the limit of 100 SOQL queries or 150 DML statements. Collect records in a collection variable inside the loop, then use one Create, Update, or Delete element after it.
Checking the effect from Apex
To confirm that a flow ran, save a record and query for what the flow should have created. This anonymous Apex assumes an after-save flow on Account that creates a Task with the account as WhatId.
Account a = new Account(Name = 'Flow Order Demo');
insert a;
List<Task> created = [
SELECT Id, Subject, WhatId
FROM Task
WHERE WhatId = :a.Id
];
System.debug('Tasks created by after-save automation: ' + created.size());Because the flow and the insert share one transaction, the flow's work is complete when insert returns. Asynchronous paths are an exception, as described below.
Errors and fault paths
An unhandled error in an after-save flow rolls back the whole transaction. The record, any records created by triggers, and earlier flow changes are all undone. The user sees the flow error message.
You can add a fault connector to an element to handle the error yourself. You can show a message or create a log record instead. Be careful when you do this. The flow continues, so the save may succeed even though one action failed. Decide on purpose whether the failure should stop the save.
To stop the save from a flow, use a Custom Error element. Prefer a validation rule when the check is a simple condition on the record.
Asynchronous paths
An after-save flow can include a Run Asynchronously path. That path runs after the commit, in its own transaction, with its own limits. Use it for work that can wait, such as callouts to external systems. If it fails, the saved record stays saved. The failure also cannot be shown to the user who made the save.
A scheduled path is similar, but it waits for a time offset relative to a date field on the record.
Ordering with other automation
Salesforce does not guarantee the order of after-save flows among themselves. You can control it, though. In Flow Builder, set a trigger order value on each record-triggered flow for an object. Flows with lower values run first, and Salesforce does not guarantee the order of flows with the same value. Flow Trigger Explorer in Setup shows the current order for each object and trigger type.
Common mistakes
- Using an after-save flow to set fields on the triggering record when a before-save flow would do.
- Placing Get Records or DML elements inside a loop.
- Leaving the "every time" option selected on a flow that updates its own record.
- Depending on the order of two after-save flows without setting trigger order values.
- Expecting an asynchronous path to be finished when the DML statement returns.
Exercise
An after-save flow on Opportunity uses an Update Records element to set a Stage-related field on the same Opportunity. The flow is set to run every time a record is updated and meets the conditions, and the condition is still true after the update. What happens, and what two changes would fix it?
Show answer
The update causes a second save of the Opportunity, which fires the triggers and evaluates the flow again. Because the condition is still true, the flow runs again and loops until a governor limit or flow interview limit stops the transaction. Fix it by changing the entry setting to "Only when a record is updated to meet the condition requirements", and by moving the field update into a before-save flow, which needs no second save.