Sub topic 13
Designing for bulk data and governor limits
This chapter assumes you can build a basic flow, know the Get, Create, Update, Delete, Assignment, and Loop elements, and understand collections. It covers how to keep flows that process many records inside Salesforce limits.
Why limits matter for scheduled and autolaunched flows
Salesforce runs on shared infrastructure, so it caps what one transaction can do. These caps are called governor limits. A schedule-triggered flow can select thousands of records, and Salesforce processes them in batches of up to 200 records. An autolaunched flow can be called from Apex or another flow with a whole collection. In both cases, a design that works for one record can fail on many.
Common per-transaction limits (synchronous) are:
| Limit | Value |
|---|---|
| SOQL queries (Get Records) | 100 |
| DML statements (Create, Update, Delete) | 150 |
| Records retrieved by SOQL | 50,000 |
| Records processed by DML | 10,000 |
| Executed elements | 2,000 |
If a transaction exceeds a limit, it fails, and Salesforce rolls back the changes made in it. Check the current values in the Salesforce documentation, because they can change between releases.
The main mistake: data elements inside a loop
Each Get, Create, Update, or Delete Records element inside a Loop runs once per iteration. With 200 records, an Update inside the loop uses 200 DML statements and breaks the limit of 150.
The bulk-safe pattern
Do the data work outside the loop. Use the loop only to change values in memory.
- Use one Get Records element before the loop to fetch all the records you need.
- Loop through the collection.
- In the loop, use an Assignment element to set field values on the loop variable.
- In the same loop, use an Assignment element to add the changed record to a second collection variable.
- After the loop ends, use one Update Records element on that second collection.
This uses one query and one DML statement, however many records are in the collection.
Other ways to stay within limits
- Filter in the query. Put conditions in the start element of a scheduled flow or in Get Records, so you retrieve only the records you need. Avoid fetching everything and filtering with a Decision inside the loop.
- Skip the loop when you can. An Update Records element can set the same field values on every record that matches filter conditions, with no loop.
- Store only what you need. In Get Records, choose specific fields instead of all fields to reduce memory use.
- Watch nested calls. A Subflow call and any Apex it triggers share the same transaction limits as the parent flow. If an autolaunched flow runs a query, calling it inside a loop multiplies those queries.
- Pass collections into subflows. Give the subflow a collection input variable, so it works on all records in one call instead of one call per record.
Example check
A scheduled flow selects 150 open cases and updates each one inside a loop. That is 150 updates, which is right at the DML limit. Add one more record, or one more DML element elsewhere in the transaction, and it fails. Moving the Update outside the loop reduces it to a single statement.
Exercise
A scheduled flow loops over 500 opportunities. Inside the loop, a Get Records element finds the owner's manager, and an Update Records element changes the opportunity. Why does this fail, and how would you redesign it?
Show answer
Salesforce processes records in batches of up to 200, so each batch runs the loop up to 200 times. That means up to 200 queries and 200 DML statements in one transaction, which exceeds the limits of 100 queries and 150 DML statements. Redesign it by running one Get Records before the loop to fetch the needed owner or manager data, using a formula or an Assignment inside the loop to set values in memory, adding each changed opportunity to a collection variable, and running one Update Records on that collection after the loop.