Sub topic 10
Preventing Recursion and Unwanted Re-triggering
This chapter assumes you can build a record-triggered flow, write entry conditions, and read $Record and $Record__Prior. It also assumes you know what a transaction is. The goal is to stop a flow from running again on changes it made itself.
Why flows re-trigger
An after-save flow that updates a record saves it again. That second save can fire the same flow, other flows, and Apex triggers on the object. If the flow's entry condition is still true, the flow runs again, and the cycle can repeat until Salesforce stops the transaction with a limit error.
A before-save flow does not have this problem. It changes the record before it is saved, so no extra save happens and nothing fires again.
Where recursion comes from
- A flow updating its own object. An after-save flow on Opportunity that updates the same Opportunity triggers the save logic again.
- Two flows on the same object. Flow A changes a field that Flow B watches, and Flow B changes a field that Flow A watches.
- A flow and Apex. An Apex trigger updates a record, which starts the flow, which updates the record, which starts the trigger.
- Parent and child updates. A flow updates a child record, and another flow updates the parent in response.
Control 1: Run only when a record is updated to meet the condition
Set the run frequency to Only when a record is updated to meet the condition requirements. The flow then runs only when the record changes from not meeting the condition to meeting it. If the flow's own update leaves the record still meeting the condition, the flow does not run again.
This setting is the first fix to try. It works well when the entry condition describes a state, such as {!$Record.StageName} = "Closed Won".
Control 2: Use ISCHANGED in the entry condition
If the condition must react to a specific change, test for that change. This formula entry condition (Formula Evaluates to True) admits an Opportunity update only when the stage has just become Closed Won:
AND(
ISCHANGED({!$Record.StageName}),
{!$Record.StageName} = "Closed Won"
)When the flow later updates a different field, StageName is unchanged in that save. ISCHANGED returns false and the flow stays out.
Control 3: Compare with $Record__Prior
You can get the same effect with $Record__Prior, which holds the values from before the save. Add a condition such as {!$Record__Prior.StageName} <> "Closed Won" next to the check on the current value. Use this when you need to compare a field to a specific earlier value, not only to detect that it changed.
Control 4: Use a marker field
Sometimes the state alone cannot tell you whether the flow already ran. Add a checkbox such as Follow_Up_Created__c. Require it to be false in the entry condition, and set it to true in the flow's update.
Entry condition:
{!$Record.StageName} = "Closed Won"
AND {!$Record.Follow_Up_Created__c} = false
Update Records element:
Follow_Up_Created__c = trueThe marker adds a field to maintain, so use it only when the first three controls are not enough.
Control 5: Prefer before-save for same-record changes
If the only job is to change fields on the triggering record, use a before-save flow. It makes no second save, so there is nothing to recurse.
Choosing a control
| Situation | Best control |
|---|---|
| Flow should run once when a state is reached | Run only when a record is updated to meet the condition requirements |
| Flow should react to one field changing | `ISCHANGED` in a formula entry condition |
| Flow needs the old value | `$Record__Prior` |
| Run-once logic that state cannot express | Marker field |
| Only changing fields on the same record | Before-save flow |
What to check when a flow loops
- Open the flow's debug run or the debug log and count how many times the flow interview starts.
- Look at which field the flow writes and whether the entry condition reads that field.
- List every other flow and Apex trigger on the object that could respond to that write.
- Tighten the entry condition so the flow's own change cannot satisfy it.
Exercise
An after-save flow on Case sets Status to "Escalated" when Priority is "High". Its entry condition is {!$Record.Priority} = "High" with the run frequency "Every time a record is updated and meets the condition requirements". It also sets a field on the same Case, and the flow keeps running. Name two changes that stop the repeated runs.
Show answer
Change the run frequency to "Only when a record is updated to meet the condition requirements", so the flow ignores saves where the record already met the condition. Alternatively, use AND(ISCHANGED({!$Record.Priority}), {!$Record.Priority} = "High") as the entry condition. Moving the same-record field change into a before-save flow also removes the second save.