Business Analysis Community Question

How do I handle DML statements in loops within a complex Salesforce Flow?

Posted on 2025-02-20 • In Business Analysis • By Christopher Vance

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?

3 Answers 18,935 Views

All answers to this question 3 Answers

Stephanie Woods Answered on 2025-04-15
Answered

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.

Brandon Lee Answered on 2025-05-02
Answered

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?

Christopher Vance Commented on 2025-05-08
Commented

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.

Amanda Scott Answered on 2025-06-20
Answered

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.

Stephanie Woods Commented on 2025-06-25
Commented

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.

Write a Comment

Your email address will not be published. Required fields are marked (*)

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.