Sub topic 10
Preventing recursion and unwanted re-triggering
This chapter assumes you can build a basic record-triggered flow. You should know the trigger events, entry conditions, and the difference between before-save and after-save. You should also know how $Record and $Record__Prior work, and that a flow runs inside a database transaction with Apex and other automation.
What recursion means for a flow
A record-triggered flow can start itself again. Saving a record fires the flows on that object. If one of those flows saves the same record, or a record that points back to it, the save fires the flows again. Each pass can cause another pass.
The platform does not stop this loop politely. The transaction continues until it hits a limit, for example maximum trigger depth, too many DML statements, or CPU time. The whole transaction then rolls back and the user sees an error. The failure often shows up only with certain data, such as a record that already meets the entry condition.
This chapter covers three kinds of unwanted re-triggering:
- Self-triggering: a flow updates the record that started it.
- Ping-pong: a flow on object A updates object B, and a flow on B updates A.
- Over-firing: the flow is not recursive, but it runs on every edit when you wanted it to run once.
Before-save flows do not re-trigger themselves
A before-save flow changes {!$Record} in memory before the database write. It does not perform a separate update, so it does not start another round of automation. This is the main reason to prefer before-save for changes to the triggering record.
Flow: Opportunity - Set Next Step (before-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Fast Field Updates
Entry condition: {!$Record.Amount} > 100000
Run: Only when a record is updated to meet the condition requirements
Assignment element
Variable: {!$Record.NextStep}
Operator: Equals
Value: Review large dealThis flow cannot loop on itself. The Assignment element only edits the record that is about to be saved.
How an after-save flow loops
An after-save flow that edits its own triggering record has to use an Update Records element, and that update is a real save. Suppose the entry condition is {!$Record.Amount} > 100000 and the run frequency is Every time a record is updated and meets the condition requirements. The flow updates NextStep on the same Opportunity.
Each pass finds Amount still above 100000, so the condition is met again. The only way out is the limit error.
Defenses, from strongest to weakest
1. Move the change to a before-save flow
If the flow only edits fields on the triggering record, make it before-save. This removes the loop at its cause. Use after-save only when you need something that exists only after the save, such as {!$Record.Id} on a new record, related records, or actions.
2. Set the run frequency to run once per change
For update triggers, the entry condition section has a run frequency option:
| Option | Behavior | Loop risk |
|---|---|---|
| Only when a record is updated to meet the condition requirements | Runs when the record did not meet the condition before the save and meets it now | Low |
| Every time a record is updated and meets the condition requirements | Runs on every update where the condition is true | High |
The first option compares the record with its prior state. A record that stays in the condition does not trigger the flow again. Use it by default and choose the second only when you need to react to every edit.
This option is not a full guarantee. If your flow, or another one, changes a field so that the condition becomes false and later true again, the flow can run again. When that can happen, add a guard from the next section.
3. Compare current and prior values in the entry condition
Use a formula entry condition that tests for a real change. ISCHANGED checks whether a field differs from its prior value. The same test works with $Record__Prior.
AND(
ISCHANGED({!$Record.StageName}),
{!$Record.StageName} = "Closed Won"
)An update made by your own flow to another field, such as NextStep, does not change StageName. So ISCHANGED({!$Record.StageName}) is false on the second pass and the flow stops.
Choose the field in ISCHANGED with care. If your flow writes to the field you are watching, you recreate the loop. Watch fields that the flow itself never writes.
4. Use a guard field
When no natural field change separates the first pass from the second, add a checkbox to the object, for example Next_Step_Set__c. Add it to the entry condition as {!$Record.Next_Step_Set__c} = false. Have the flow set it to true in the same update that does the work.
A guard field costs a custom field and extra logic. It also holds state in the data, so you need a plan for when it resets. For example, you may want the flow to run again when the Amount changes. Prefer the options above when they are enough.
5. Limit what the update touches
When the after-save flow must update the triggering record, set only the fields that need it. Use an Update Records element that targets the record from the trigger, not a Get Records element that reads the record again and writes back everything. Fewer fields written means fewer chances to touch a field that other automation watches.
Ping-pong between objects
Cross-object loops are harder to see because each flow looks correct alone. For example, a flow on Opportunity updates a field on the parent Account, and a flow on Account updates all child Opportunities.
To find the problem, draw the chain: which object each flow updates and which flows listen to that object. Then break it at one point:
- Make each entry condition depend on a field that only the originating change touches.
- Use "Only when a record is updated to meet the condition requirements" on both flows.
- Make one side before-save, or remove one of the two updates.
- Replace the second hop with a scheduled or asynchronous path, so that it runs in a separate transaction with its own limits.
Recursion across Apex and flows
A flow does not share state with Apex triggers on the same object. An Apex trigger can update a record and fire your flow, and your flow can update a record and fire the trigger. Apex developers often guard triggers with a static variable. That guard does not protect the flow. The flow still runs on each save unless its own entry condition says no. When Apex and flows share an object, review both sides of the loop.
Test that the flow runs once
The after-save flow below creates a follow-up Task when an Opportunity is updated to Closed Won.
Flow: Opportunity - Create Follow-up Task (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
Create Records element (object: Task)
Subject: Send welcome package
WhatId: {!$Record.Id}
OwnerId: {!$Record.OwnerId}Because of the run frequency, later edits to a Closed Won Opportunity should not create more Tasks. This Apex test checks that. It assumes the flow above is active.
@IsTest
private class OpportunityFollowUpTaskTest {
@IsTest
static void taskIsCreatedOnlyOnce() {
Opportunity opp = new Opportunity(
Name = 'Renewal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 5000
);
insert opp;
opp.StageName = 'Closed Won';
update opp;
opp.Amount = 6000;
update opp;
opp.Amount = 7000;
update opp;
Integer taskCount = [
SELECT COUNT()
FROM Task
WHERE WhatId = :opp.Id AND Subject = 'Send welcome package'
];
Assert.areEqual(1, taskCount, 'The flow should create one Task only');
}
}A test like this catches a wrong run frequency. If the flow ran on every update, the count would be 3. In Flow Builder, use the debug run and the flow's debug logs to see how many times an interview starts in one transaction. Several interviews of the same flow for one record signal a loop.
Common mistakes
- Using an after-save Update Records element on
{!$Record}when a before-save Assignment would do. - Leaving the run frequency on "Every time" without a guard.
- Watching a field in
ISCHANGEDthat the same flow writes. - Testing only with new records, so the update path never runs.
- Testing one flow alone and missing a loop that needs two flows or an Apex trigger.
Exercise
An after-save flow on Case has the entry condition {!$Record.Priority} = "High" and the run frequency "Every time a record is updated and meets the condition requirements". It updates the same Case to set Escalated__c to true. Why does it risk a loop, and give two ways to fix it.
Show answer
The Update Records element saves the Case, which fires the flow again. The Case still has Priority High, so the entry condition is met again on every pass, until a limit error stops the transaction. Fixes: (1) make it a before-save flow and set {!$Record.Escalated__c} with an Assignment element, which needs no separate save; (2) change the run frequency to "Only when a record is updated to meet the condition requirements"; (3) add {!$Record.Escalated__c} = false to the entry condition, or use ISCHANGED({!$Record.Priority}), so the second pass does not qualify.
Exercise
A flow on Opportunity updates Account.Last_Deal_Value__c. A flow on Account updates Opportunity.Account_Tier__c on all child Opportunities when any Account field changes. What problem can occur, and how would you break it?
Show answer
This is ping-pong. Each flow's update fires the other flow, so the changes bounce between the objects until a limit is hit. Break it by making the Account flow's entry condition depend on a field that the Opportunity flow does not write, for example ISCHANGED({!$Record.Tier__c}), and by using "Only when a record is updated to meet the condition requirements". You could also move one of the updates to an asynchronous or scheduled path so it runs in a separate transaction.