How do I handle DML statements in loops within a complex Salesforce Flow?
I’m building a Flow to update related Opportunity Line Items when an Account is updated, but I keep hitting the "Too many DML statements: 151" governor limit. I know the golden rule is "Never put a Pink element in a Loop," but I’m struggling to properly bulkify my logic. Can someone walk me through the correct way to use a Collection Variable and the "Assignment" element to stage these updates so I only use one "Update Records" call at the end?
2025-02-20 in Business Analysis by Christopher Vance
| 18914 Views
All answers to this question.
This is a classic "Admin to Developer" hurdle. Inside your loop, you should only use an Assignment element. First, create a single Record Variable (e.g., var_SingleOLI) and a Record Collection Variable (e.g., col_OLIsToUpdate). Inside the loop: 1. Assign the new values to var_SingleOLI. 2. Use a second Assignment element to "Add" var_SingleOLI to col_OLIsToUpdate. 3. After the loop finishes (the "default" path), place one single Update Records element and point it to the entire col_OLIsToUpdate collection. This consumes only 1 DML statement regardless of whether you are updating 5 or 500 records, which is the key to scalable Flow design.
Answered 2025-04-15 by Stephanie Woods
Are you also checking for null values or empty collections before that final Update element to prevent the Flow from crashing when no records meet the criteria?
Answered 2025-05-02 by Brandon Lee
-
Brandon, I actually wasn't doing that, and it's a great point. I’ve added a Decision element right after the loop to check if col_OLIsToUpdate is "Is Null: False." This has already saved me from several "unhandled fault" emails today. It’s funny how these small "defensive programming" steps make the whole system feel so much more professional and robust for the end-users.
Commented 2025-05-08 by Christopher Vance
Always use the "Debug" button in Flow Builder with the "Rollback" option enabled so you can test your bulk logic without actually messing up your sandbox data.
Answered 2025-06-20 by Amanda Scott
-
Good advice, Amanda. The new 'Expandable Debug' view in the Summer '24 release makes it even easier to track how those collection variables are growing in real-time.
Commented 2025-06-25 by Stephanie Woods
Write a Comment
Your email address will not be published. Required fields are marked (*)

