Sub topic 9
Testing Flows with Bulk Data
This chapter assumes you can build a record-triggered flow, read basic Apex, and have a sandbox or Developer Edition org. It also assumes you know how to write a single-record Apex test that exercises a flow, which the previous chapter covered.
Why bulk testing matters
A flow that works for one record can fail when many records arrive together. Data Loader, an API call, or an Apex update can send up to 200 records to a record-triggered flow in one transaction. Salesforce runs the flow once for that batch. The flow's interviews share the transaction's governor limits.
Flow Builder's debugger and manual clicks test one record at a time. They do not reveal a Get Records or Create Records element inside a loop. That pattern uses one query or DML statement per record. With 200 records, it hits a limit such as 100 SOQL queries or 150 DML statements.
What bulk tests expose
- A Get Records, Update Records, or Create Records element placed inside a Loop. This is the most common cause.
- An Apex action that is not bulk-safe and runs queries per call.
- Recursion, where one flow's update triggers another flow or trigger on the same records.
- A flow that is correct for one record but mishandles a collection, such as using only the first item.
Writing the bulk test
This test assumes an active record-triggered flow that creates a follow-up task when an opportunity becomes Closed Won.
@IsTest
private class OpportunityFlowBulkTest {
@IsTest
static void flowHandlesTwoHundredRecords() {
Account acc = new Account(Name = 'Bulk Test Account');
insert acc;
List<Opportunity> opps = new List<Opportunity>();
for (Integer i = 0; i < 200; i++) {
opps.add(new Opportunity(
Name = 'Bulk Opp ' + i,
AccountId = acc.Id,
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
));
}
insert opps;
Test.startTest();
for (Opportunity o : opps) {
o.StageName = 'Closed Won';
}
update opps;
Test.stopTest();
Integer taskCount = [SELECT COUNT() FROM Task WHERE WhatId IN :opps];
Assert.areEqual(200, taskCount, 'Each opportunity should get one task without hitting limits');
}
}Several details matter here:
- Build the records in a list and use one DML statement. Calling
updateinside a loop would run the flow 200 times with one record each. That would not test bulk behavior. - Wrap only the action under test in
Test.startTest()andTest.stopTest(). These calls give the code between them a fresh set of governor limits. Setup work beforestartTestdoes not count against the flow. - Assert on the outcome. A test that only checks that no exception occurred can pass while the flow silently skips records.
- Count with
COUNT(). It avoids loading 200 rows when you only need a number.
Reading a failure
When a flow breaks a limit, the test fails with a message such as Too many SOQL queries: 101. The message names the limit but not the element. To find the cause:
- Open the flow in Flow Builder and look for data elements inside loops.
- Run the test with a debug log at the Workflow level set to FINER, then search for repeated
FLOW_ELEMENT_BEGINlines for the same element. - Count how many times that element ran. Roughly one run per record confirms a query or DML inside a loop.
The fix is to move the data element outside the loop. Query once before the loop. Inside the loop, only modify variables or add records to a collection. After the loop, run a single Update Records or Create Records element on the collection.
What to vary
One bulk test is a start. Try these variations:
- Mix records that meet the flow's entry criteria with records that do not, to check that the flow only processes the right ones.
- Spread records across several accounts if the flow queries related records.
- Run the same bulk test for insert, update, and delete if the flow triggers on more than one of them.
Exercise
A bulk test updates 200 opportunities with a loop that calls update o; once per record. It passes, but the real flow fails on a data load. Why did the test miss the problem, and how do you fix it?
Show answer
Each update o; is a separate transaction step that runs the flow with one record, so the flow never receives a batch. Collect the records in a list, change them in a loop, and call update opps; once after the loop. Wrap that single update in Test.startTest() and Test.stopTest(), then assert on all 200 results.