Flows can do almost everything these days, the question I always had was what can flows NOT do? Why not use flows to replace Apex completely?
I only found the answer to this question when digging deeper into flows. Unfortunately flows have some limitations.
Number of iterations limit
I only knew about this flow limitation when I hit the Number of Iterations Exceeded error. Apparently flows has a limit of 2000 executed elements at run time. Let’s look on this flow to understand this better.
This is a flow trigger that runs on Case update, it contains 6 elements. According to salesforce documentation, the loop element is the reason we could hit this limit. It’s because each element in the loop is multiplied by the number of records that you’re looping on to produce the number of iterations.
Let’s try calculating the number of iterations in our flow here. Assume that we’re looping on 600 files.
Number of iterations:
Get Records element + Decision element + [600 * 3 ( Loop + Assignment + Assignment)] + Update Records element = 1803
As we mentioned, the total value should be less than or equal to 2000. In our case (600 records) the flow will not break but if the iterations exceed 2000, you’ll notice Number of Iterations Exceeded error.
Apex to the rescue
To solve this limitation I have replaced the Loop element and the 2 Assignment elements with an Apex Action that calls an Invocable Method in apex. So although it could be done in a flow, I had to use apex because of this limitation and because in my use case I had a lot of records to loop on. This is one of the ways that you can overcome this error.
So the next time you need to perform multiple operations in a loop or you have to iterate on a lot of records, consider using apex instead.