Sub topic 11
Asynchronous and Scheduled Paths
This chapter assumes you can build an after-save record-triggered flow. You should know entry conditions, $Record, and the Create Records element. You should also know the basics of a transaction and governor limits, which earlier chapters covered. The chapter covers the two extra path types an after-save flow can have, what each one changes, and how to choose between them.
Why paths exist
A record-triggered flow normally runs everything in the same transaction as the save that triggered it. That has two costs:
- The user waits for all the flow's work to finish.
- The flow shares one set of governor limits with Apex triggers, other flows, and the rest of the transaction.
Paths let you move part of the work out of that transaction. An after-save flow can have up to three kinds of path:
| Path | When it runs | Transaction | Available in |
|---|---|---|---|
| Run Immediately | During the save | Same as the triggering save | Before-save and after-save |
| Run Asynchronously | Soon after the save commits | Separate transaction | After-save only |
| Scheduled Path | At a time you define, relative to a date or the current time | Separate transaction | After-save only |
Run Immediately is the path you already use. Before-save flows have only that path, because they run before the record is committed and cannot hand work to a later transaction. Flows triggered by record deletion also cannot use asynchronous or scheduled paths. If you cannot see the option in Flow Builder, check the trigger event and the optimization setting first.
Run Asynchronously
An asynchronous path starts after the original transaction commits. It does not make the flow faster in an absolute sense. The work still happens, but the user no longer waits for it, and it gets a fresh set of governor limits.
Use it when:
- The work is slow, such as a callout to an external system through an action.
- The transaction is already close to its limits and the flow's work is not needed right away.
- A failure in the follow-up work should not roll back the user's save.
That last point matters. In a Run Immediately path, an unhandled fault rolls back the whole transaction, including the user's change. In an asynchronous path, the original save is already committed. The user will not see the error. It surfaces as a failed flow interview and, depending on your settings, an error email.
You configure it on the Start element. Select the path option Run Asynchronously, and the flow gives you a new branch to build from.
Flow: Opportunity - Notify Billing System (after-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Actions and Related Records
Entry condition: {!$Record.StageName} = "Closed Won"
Run: Only when a record is updated to meet the condition requirements
Path: Run Immediately
Create Records element (object: Task)
Subject: Send welcome package
WhatId: {!$Record.Id}
Path: Run Asynchronously
Action element: Call external billing service
Input: {!$Record.Id}The immediate path does the fast, local work. The asynchronous path handles the slow callout, so the callout cannot delay or roll back the save.
What changes inside an asynchronous path
- Limits. The path has its own transaction, so its SOQL queries, DML statements, and CPU time are counted separately from the triggering save. Asynchronous limits are higher for some resources, and some org-wide daily allocations still apply. Check the current limits documentation for your edition.
- Timing. There is no guarantee about when it starts. Do not build logic that assumes it has run by the time the user sees the next page.
- Data freshness. Other automation may change the record between the save and the asynchronous run. Read values you depend on from
$Recordat the start of the path and add a Decision element if the logic is sensitive to the current state. - Running user. Asynchronous work may not run in the same user context as the save. If your logic depends on
$Uservalues or sharing, test it with a real user, not only as an administrator.
Scheduled Paths
A scheduled path runs at a time relative to a date or a point in time. You choose a time source, an offset, and a direction.
- Time source: a date or date/time field on the triggering record, such as
CloseDate, or the moment the record was created or updated ($Flow.CurrentDateTime). - Offset: a number and a unit, such as 3 days or 2 hours.
- Direction: before or after the time source.
Some examples:
- Create a reminder task 7 days before
CloseDate. - Send a survey email 3 days after the record is updated to Closed Won.
- Escalate a case 4 hours after it is created, if it is still open.
Flow: Opportunity - Close Date Reminder (after-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Actions and Related Records
Entry condition: {!$Record.IsClosed} = false
Run: Every time a record is updated and meets the condition requirements
Scheduled Path: Reminder Before Close
Time source: {!$Record.CloseDate}
Offset: 7 Days Before
Decision element: Still open?
Outcome: {!$Record.IsClosed} = false
Create Records element (object: Task)
Subject: Close date in 7 days
WhatId: {!$Record.Id}
OwnerId: {!$Record.OwnerId}How a scheduled path behaves
Salesforce evaluates the entry conditions when the record is saved and queues the path for the computed time. When the time arrives, the path runs in its own transaction. Several points follow from that:
- The flow can run long after the record changed. Do not assume
$Recordstill matches what the user saved. Start the path with a Decision element that re-checks anything the action depends on, like theIsClosedcheck above. - Run frequency interacts with the schedule. If the flow runs every time the record is updated, each qualifying update can queue a new run. The effect on already-queued runs depends on your entry conditions. Test it with real data and confirm whether you get one reminder or several. This is the same recursion and re-triggering concern from the previous chapter, stretched over time.
- A time source in the past. If the computed time is already past when the record is saved, the path is typically scheduled to run as soon as possible. Do not rely on it being skipped.
- Records are processed in batches. Many records can reach the same scheduled time, for example every opportunity with the same close date. The flow processes them in bulk, so the path must be bulk-safe. Avoid per-record queries or DML inside loops.
- Running user. Scheduled paths usually run as the Automated Process user or a similar system context, not as the person who saved the record. Fields such as
$User.Idor$Record.LastModifiedByIdmay not mean what you expect.
Choosing a path
| Requirement | Best fit |
|---|---|
| Set a field on the triggering record | Before-save flow (no path needed) |
| Create a related record the user needs to see right away | Run Immediately |
| Call an external system, and the save must not fail because of it | Run Asynchronously |
| The transaction is near its limits and the work can wait | Run Asynchronously |
| Do something N days before or after a date | Scheduled Path |
| Do something on a fixed calendar for many records | Scheduled-triggered flow |
You can mix paths in one flow. A common pattern is an immediate path for local work, an asynchronous path for the callout, and a scheduled path for a follow-up.
Faults, testing, and debugging
Failure handling differs by path:
- Run Immediately: an unhandled fault rolls back the save and the user sees an error.
- Asynchronous and scheduled: the original save is already committed. A fault affects only that path's transaction. Nobody is looking at the screen, so add a Fault connector on each element that can fail. Route it to something visible, such as creating a log record or sending an email to an admin.
For testing:
- Flow Builder's Debug is built for the immediate path. Do not treat a clean debug run as proof that the asynchronous or scheduled paths work.
- To test an asynchronous path, create or update a real record in a sandbox, then check the results and any failed flow interviews. In Apex tests, the asynchronous work is typically processed when
Test.stopTest()is called, so put your assertions after it. Confirm this behavior in your org's release. - To test a scheduled path, use a short offset such as a few minutes after
$Flow.CurrentDateTimein a sandbox. Remove or lengthen the offset before deploying.
@IsTest
private class OpportunityAsyncPathTest {
@IsTest
static void asyncPathCreatesFollowUp() {
Opportunity opp = new Opportunity(
Name = 'Async test',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
);
insert opp;
Test.startTest();
opp.StageName = 'Closed Won';
update opp;
Test.stopTest();
Integer taskCount = [
SELECT COUNT() FROM Task WHERE WhatId = :opp.Id
];
Assert.isTrue(taskCount > 0, 'Expected the flow to create a Task');
}
}This test assumes an active flow like the Closed Won examples earlier in this tutorial. Adjust the assertion to match what your own path produces.
Common mistakes
- Looking for Run Asynchronously on a before-save flow. It exists only on after-save flows.
- Putting work the user must see immediately on an asynchronous path. The user may refresh before it runs.
- Assuming
$Recordis current when a scheduled path runs. Re-check with a Decision element. - Leaving a scheduled path with no Fault connector. Failures happen later and go unnoticed.
- Forgetting that a path shares the flow's trigger. A Create or Update inside an asynchronous path can fire other automation and cause re-triggering.
Exercise
An after-save flow on Opportunity must call an external pricing service when an opportunity becomes Closed Won, and a reminder task must be created 5 days before CloseDate on every open opportunity. The pricing service is slow and sometimes times out. Which paths would you use for each requirement, and why?
Show answer
Put the pricing callout on a Run Asynchronously path. It then runs in a separate transaction with its own limits, and a timeout does not roll back the user's save. Add a Fault connector there to log failures. Put the reminder on a Scheduled Path with CloseDate as the time source and an offset of 5 days before. Start the scheduled path with a Decision element that checks the opportunity is still open, because the record may have changed before the path runs. Any local work the user needs to see immediately belongs on Run Immediately.
Exercise
Why can a before-save flow not use an asynchronous or scheduled path, and what is the practical consequence for a requirement like "send an email 3 days after a field changes"?
Show answer
A before-save flow runs before the record is committed and exists only to change the triggering record's fields without extra DML. It has no later transaction to hand work to, so only Run Immediately is available. For a delayed email, use an after-save flow with a Scheduled Path (3 days after $Flow.CurrentDateTime, or after a relevant date field) and an action that sends the email.