When to call an Apex Action from a Flow?

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.

--

--

--

Salesforce Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Moving Away from Monolithic Architecture 👋🏾

🤑9 Black Friday deals👍 for Learners🧑‍🏫 and Developers 🧑‍💻🤑

Building a Responsive Web Page with Quest AI and Figma

Get excited for Redis Enterprise on Azure

How to install routeros on Ubuntu

Mounica’s Journey to a Google Internship

Calling Python and C++ Code using PyBind

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Farah Sherif Ghanem

Farah Sherif Ghanem

Salesforce Developer

More from Medium

Making YOU Shine

Salesforce Year 2021 Highlights

Leading International Consumer Brand Uses Mambo Merge to Streamline Support Operations.

#nocode Progress Bars You Can Create Today on ANY Object