back to blog
AVOID SOQL/DML LIMITS IN FLOWS
January 16 2023 • 5 min read

BUSINESS CHALLENGE

Based on the requirement we may have to use loop elements in Salesforce flows, and use Get Records or Update Records elements which might exceed governor limits.

Since Salesforce is a multi-tenant platform it has Per-Transaction Flow Limits which includes the SOQL(100) and DML(150) limits. When these limits exceed during the flow execution, the flow throws errors and will not work as expected.

CONSIDER THE FOLLOWING STEPS TO AVOID THE LIMITS

  • The Get Records and the Update Records elements should never be placed inside the loops.
  • Fetch all the records needed using the Get Records element outside the loops and store them in a collection variable.
  • Filter the required records or related records from the collection using the Collection Filter element.
  • Create/Update records elements should be outside of the loops.

SCENARIO

Here is an example of how you can use the Collection filter element to avoid using the Get Records element in a loop.

The below scheduled triggered flow sends an email to the Opportunity Owner when the close date falls in any of the days from the upcoming week with the Opportunity information and Account Name in the email body.

You can see the same flow built in two different ways below, using the Get records element inside the loop and outside the loop.

When Get Records element is used inside the loop

dml blog image 1

When any Get Records element or Update Records element is used inside the loop element, the System displays a tip which includes the best practice to be followed.

In the above flow we are looping through the Opportunity records which meet the criteria and then pulling the Account related to that specific Opportunity. Then we use the Opportunity details and Account Name in the email body to send the email to the Opportunity Owner.

If there are more than 100 Opportunities in the Org, then the Get Records element will be executed more than 100 times and  the governor limits will exceed because the limit for the number of SOQL queries is only 100.

dml blog image 2

 

When Get Records element is used outside the loop

dml blog image 3

Instead of using the Get Records element inside the loop to collect the Account records, we are looping all the Opportunity records and collecting the Account Ids in a collection and then we use a different Get Records element to fetch all the Account records using the Account Ids collection.

Next, we loop through all the Opportunity records again and use the Collection Filter element to fetch the Account record using the AccountId of the Opportunity record.

When we filter records from a collection, it creates another collection even if the resulting record is only one. So, we loop through that collection and use the Account Name and Opportunity details in the email body to send the email to the Opportunity Owner.

Similarly the records that are being updated or created in loops should be added to different collection variables and then those collections should be updated outside the loop using the Update Records/Create Records element.

WRAPPING IT UP

In this blog we have covered how to prevent using SOQL/DML operations inside the Loops using the Collection Filter Element which helps us avoid exceeding the governor limits.

Leave a Comment

Your email address will not be published

© 2024 Digital Biz Tech