Sub topic 15
Keeping flows bulk-safe and avoiding recursion
This chapter assumes you can build a basic record-triggered flow and know the entry conditions and run frequency options from earlier chapters. It covers two problems that appear after a migration: flows that hit governor limits when many records change at once, and flows that trigger themselves.
Why bulk safety matters
Data loads, integrations, and list-view edits change many records in one transaction. Salesforce processes up to 200 records per batch. A record-triggered flow receives the whole batch. The platform bundles the interviews so that Get Records, Create Records, Update Records, and Delete Records elements run once for the batch, not once per record.
That bundling stops at a loop. Elements inside a Loop run once per iteration. The relevant limits apply to the whole transaction: 100 SOQL queries, 150 DML statements, and 10,000 DML rows.
The loop pattern
Never put a Get Records or a DML element inside a Loop. Query first, loop to assemble changes, then write once.
Workflow Rules and Process Builder hid this from you. Migrated flows often gain loops, for example when a process updated child records. Check each loop for queries or DML.
Choose before-save when you can
A before-save flow that updates fields on the triggering record needs no DML element and does not fire other automation again. This is the cheapest option for limits and the safest for recursion. Use after-save only when you need related records, emails, or actions outside the record.
Recursion
Recursion happens when a flow's own update fires the same flow again. It usually appears in after-save flows that update the triggering record or its parent. Process Builder had a setting for this. Its "Allow process to evaluate a record multiple times" option controlled re-evaluation. In Flow you control it with three tools:
- Set the run frequency to Only when a record is updated to meet the condition requirements, so the flow runs only when the condition changes from false to true.
- Add a condition that your own update makes false. Compare
{!$Record.StageName}with{!$Record__Prior.StageName}, or test a flag field that the flow sets. - Move same-record field updates into a before-save flow.
Prove it with a bulk test
Test with 200 records, as the Apex test below does. It assumes the migrated flow creates one Task when an Opportunity becomes Closed Won. Adapt the stage and the assertion to your flow.
@IsTest
private class ClosedWonFlowTest {
@IsTest
static void bulkClosedWonCreatesOneTaskEach() {
List<Opportunity> opps = new List<Opportunity>();
for (Integer i = 0; i < 200; i++) {
opps.add(new Opportunity(
Name = 'Test ' + i,
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
));
}
insert opps;
for (Opportunity o : opps) {
o.StageName = 'Closed Won';
}
Test.startTest();
update opps;
Test.stopTest();
System.assertEquals(200, [SELECT COUNT() FROM Task WHERE WhatId IN :opps]);
}
}A limit error here means a query or DML sits inside a loop. A count above 200 points to recursion, because the flow ran again and created extra Tasks.
Exercise
A migrated after-save flow updates a field on the Opportunity that triggered it, and it creates duplicate Tasks on each save. Name two changes that fix this.
Show answer
Set the run frequency to "Only when a record is updated to meet the condition requirements" so the flow ignores later saves that do not change the condition. Alternatively, move the field update into a before-save flow, which does not re-trigger the flow. Making the update falsify the entry condition also works.