{{"error_type": "AttributeError"}}HTTP/1.1 200 OKServer: Zope/(2.13.26, python 2.7.14, linux2) ZServer/1.1 Date: Sat, 16 Jan 2021 12:25:46 GMT Content-Length: 34029 Content-Language: en-us Expires: Sat, 1 Jan 2000 00:00:00 GMT X-Ua-Compatible: IE=edge X-Frame-Options: SAMEORIGIN Content-Type: text/html;charset=utf-8 Flow Bits – Keeping your DML Outside of the Loop — Arkus, Inc.

Flow Bits – Keeping your DML Outside of the Loop

07/15/2020 by James Bujold
Flow Builder is a powerful declarative automation tool that can be used in countless ways to improve your organization’s Salesforce user experience. This post is part of a series of helpful hints and tips to help you build better flows.  If you’re new to Flow Builder, start here.

DML

Today we are addressing the importance of keeping Database Manipulation Language (DML) operations outside of your loops within a flow.  DML refers to anytime your flow directly interacts with your Salesforce data.  Each of these operations is represented by its own element in Flow Builder. Each of these operations can be performed on one or many records at once.  The limit for DML operations within a single flow is 100.

Flow-1.png

Loops

Loops in a flow are created in two different ways.  First is the Loop element. 

Flow-2.png

The loop element takes each item within a collection and passes it individually through the operations set up inside the loop.  Once all records have been passed through the loop it continues with any additional steps ‘After last item’. 

Another way to create a loop is with a Decision element.  In the example below, as long as the NumberOfPayments variable is greater than 0 the flow will keep creating new records and adding them to a collection. 

Flow-3.png

Flow-4.png 

 As the record gets added to the collection we use the same assignment record to reduce the NumberOfPayments variable by 1.  Eventually, the loop will end once that variable is reduced down to 0.

Flow-5.png

The 100 Query Limit

In each of the example loops above, every collection item undergoes every operation inside that loop. If one or more of the DML elements is placed within the loop every item in that collection will trigger a DML transaction and count against the 100 query limit. In the examples below the two loops are identical except the loop on the left updates each record as it passes through the loop while the loop on the right adds each updated record to a collection and then executes a mass update of the entire collection in one DML execution.  

Flow-7.png

For comparison, the loop on the left could never pass more than 100 records through it. The loop on the right could pass 1000s of records as the only limit here would be the total limit for transactions within a flow being 20,000.  

Next time you are building a flow, keep an eye on where you are placing your DML operations and avoid putting these elements within a loop.  This will keep your flows much more efficient and drastically increase the number of records the flow is able to process at once.

Look for more Flow Bits in upcoming blog posts. How are you using flows? Please feel free to comment directly at me in the Salesforce Trailblazer Community or on Twitter @jpbujold.