If you’ve worked with Salesforce flows often enough, you’ve more than likely run into one of these painful errors!
Apex CPU time limit exceeded
or
Too many SOQL queries: 101
or
Too many DML operations.
While these errors are really frustrating to encounter, they are unfortunately part of dealing with a multi-tenanted system like Salesforce to ensure that one tenant does not exceed the limits allocated and cause performance issues for all the other tenants.
Ok, so we want to be good tenants, but we also need a way to process larger data sets or handle larger record operations. You could always contact the Salesforce certified experts at XTIVIA to write some batch APEX for you to handle all of this in the background, but what about an easier declarative approach without having to write any code, especially if its a process that changes frequently?
The solution is actually fairly easy…..use the PAUSE element in your flow!
The pause element essentially allows you to delay the processing of the next step in the flow until a specific time or a date has elapsed, or a Platform Event Message has been received.
Well great… but how does the pause element help overcome a transaction limit you ask? Well, the magic behind this element is buried in the documentation for it:
A transaction ends as soon as a flow interview pauses for one or more resume events. When the flow interview resumes, a new transaction begins. Everything after the pause element is executed as part of a batch transaction that includes other resumed interviews.
In plain English, once the pause is hit, the subsequent operations are done as a NEW transaction and your limits are all reset! Throw another 100 SOQL queries at the system, it won’t complain!
In the example below, a pause element is being used inside a loop that processes multiple contracts every night and performs complex operations on each contract.
The flow would typically fail if you had to process more than 4 or 5 contracts, but with the pause element in place, it can potentially process 100s of contracts daily, just not all at once! The pause would halt the process for an hour (which is the minimum timeframe Salesforce allows for at the time of this writing) and then continue processing the remaining records.
This way we can process several hundred records as part of a single flow run, and yet not exceed transaction limits!
You can get creative with this too! If you have a 100 records to process, you could keep a flow variable that acts like a counter that tracks how many records have been processed and if you have hit 20 records, you force the flow to pause, resume an hour later, (reset the variable counter back to 0) and process the next 20 records!
You can also use multiple pause elements in your flow. If you have several DML statements or SOQL operations, its easy to break them up across multiple pause elements in your flow to ensure they are run across multiple transactions.
Pro tip!
You should add fault paths for DML statements so that if the fault message contains too many SOQL queries or too many DML operations, the flow returns to the pause element which will gracefully handle the error.
Important Caveats:
It is important to note that the pause element can only be used on flows that are autolaunched.
And, if you’re an active reader of our Salesforce blogs, you’ve probably already read our recommendation that autolaunched flows should be the only flow type you should ever use. (Hyperlink: https://www.xtivia.com/blog/salesforce-autolaunched-flows) So, this is yet another reason to use autolaunched flows as often as possible!
Flow interviews when paused are resumed as part of a batch that could include other paused flows. These paused flows can also run as the same user and potentially exceed the apex governor limits as well, so this is something to be aware of.
There are other important considerations which are outlined in the salesforce documents (linked below).
Well there you have it… hit pause and get around those pesky governor limits!
Link to the official Salesforce documentation on the Pause Flow Element https://help.salesforce.com/s/articleView?id=sf.flow_ref_elements_pause.htm&type=5
Link to very useful considerations when using the Pause Flow:
https://help.salesforce.com/articleView?id=flow_considerations_design_pause.htm&type=0