top of page

How to avoid hitting Flow limits?

Feb 4, 2020
3 min read

Hi, very happy to see you here! 😀

As the title of the post already describes, today we shall learn how we can work our way around flow limits, if necessary.

Let’s set the Tempo

One of the common Flow error, which occurs because of a flow limit, is: ‘Number of Iterations Exceeded’. I’m sure a lot of you must have come across this one. So let’s see how we can avoid hitting a flow limit that says: “At run time, the maximum number of elements that can be executed is 2000.” Ref: Limits for Visual Workflow. There is also an idea related to this limit, feel free to upvote: https://success.salesforce.com/ideaView?id=0873A0000015C2KQAU

You are more likely to hit this error if have loop elements in your flow, looping over large collections.

Okay, we get it! But how do we resolve it?

I would suggest start with basic optimization. For example, see if you can reduce the number of elements by combining similar elements, or by making use of formula variables.

But what if that doesn’t cut it?

Well, here comes the interesting part. Each flow interview runs in the context of a flow transaction. And the way flow engine is designed, it says that a flow transaction ends when:

  • A Screen, Local Action, or Pause element is executed

  • The order of execution has completed—if the flow was triggered when a record was created or updated

  • All the interviews in the transaction have finished

Now pay attention to the first bullet point. That’s the key to your solution.

Now all you have to keep in mind is when to use what and that depends on what is the type of your flow. In addition to this, what’s very important is that you will have to maintain the count of executed elements in a variable. And by checking the value of the counter variable in a decision element, you will know when to ‘cheat’ the limit.

Now, if your flow is an Autolaunched flow, then it’s a simple choice. Use the Pause element, and configure it to resume your flow after 0 hours of current date-time. Please note that pausing the flow makes the rest of the flow run asynchronously in a separate transaction.

Now, what if your flow is a Screen flow? Well in that case, you have two options to ‘end’ a flow transaction, namely: a Screen element or a local Flow action.

You already know what a screen element is. So if adding a screen element(to end the transaction) fits into the business logic that you are trying to implement, then you can use that accordingly by checking the value of the counter variable.

But, if adding a Screen element makes it a bit weird or awkward or ridiculous, then you should use a local Flow action.

A local Flow action is essentially a lightning component which is available to be used as a flow action. So to help you cheat the limit, you could create a ‘dummy’ local flow action and use that accordingly by checking the value of the counter variable.

The lightning component(and JS controller) will look something like this: DummyFlowLocalAction.cmp

<aura:component implements="lightning:availableForFlowActions" access="global"> <!-- Blank component --></aura:component>

DummyFlowLocalAction.js

({ // When a flow executes this component, it calls the invoke method invoke : function(component, event, helper) { /* This is just a dummy action, so we can just keep it blank. */ //console.log('Dummy flow action called.'); }})

Once you have created this lightning component, you will be able to use it in your flow via Action element.

Congratulations, you just worked your way around one of the Flow limits! 👏 😎

Don’t forget to read me!

As pointed out by Jeroen in the comments section, this is NOT the recommended path. It certainly doesn’t follow any best practice standards and is more like a hacky way to get around the limits. It’s good to have knowledge about this but doesn’t necessarily mean you should use them in designing solutions.

Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

 
 
 

Comments


bottom of page