A few days ago I read in Dan Appleman’s book, Advanced Apex Programming, that “all of your Apex code should be designed to handle bulk operations. All of it – no exceptions” which shot me off on a tangent yesterday to see if I could “bulkify” my Get RecordType Id Flow (Lookup RecordType Ids in Visual Flow – A “Bulkified” Approach). My solution ended up being a combination of three Flows, with the third one returning the Id at a specified index from a collection of Ids.
As I was putting the finishing touches on that project it dawned on me that the third flow could be a generic utility Flow that simply returns the nth element of any input collection. Instead of having a Flow that only works on Id (Text) collections (and is named in such a way that I may not think to use it in other situations), why not have one that will work on all sorts of collections?
Overview of the Flow
So far this Flow handles two types of collections: Text collections (elements are outlined in green) and Number collections. I really only need it to handle Text collections right now, but for the exercise of figuring out how to make it work I included handling Number collections.
In order for this Flow to handle different types of collections (Text, Number, SObject, etc) it must have input collection variables for each type. Since mine, so far, handles only two types of collections I have two input collection variables. You could add all sorts of collections to your Flow including collections of SObjects, like Accounts and Contacts.
Other than counters the Flow also will need an input variable to receive the requested index, output variables for each collection type and variables to use in your Loop elements.
The Flow of the Flow
The first thing it does is count the number of elements in one of the input collections. The collection that the Flow starts with is arbitrary – mine just happens to start with the Text collection.
If the Flow determines that there are no elements present it proceeds to the next collection.
If elements are present, there is a quick check to make sure that the requested index is within the number of elements in the collection.
If the requested index is invalid the Flow assigns a message to an output variable that the calling Flow can choose to receive and check.
If valid, another loop parses through the collection and saves the nth element to a result variable.
Using the Flow
Here is an example of how you would use the Flow to retrieve the 2nd element of a text collection:
And how you would get your result back:
Optionally you can also ask for the number of elements in the input collection, and an error message:
Some Additional Details
For my purposes I’ve exposed the count of the number of elements in the collection to the calling Flow (ElementCount is an output variable). This is totally optional. I have another utility Flow that counts the number of elements in a collection that I could use but if there was ever a need to know the number of elements and retrieve a specific element this Flow would do both in one fell swoop.
And here is an alternate way to handle the end of the flow for any given collection:
In this ending, as soon as the result is located the Flow exits. Unless you’re working with extremely large collections this ending probably won’t save any time, but for what it’s worth, it does increase the efficiency of the Flow some infinitesimal amount.
A word of caution about exiting a loop prior to parsing the entire loop. If you do this for a loop within a loop you’ll get some really inaccurate results. This is because Salesforce doesn’t begin looping through a collection at the beginning of it if you’ve previously exited it somewhere in the middle. This was a situation I discovered very early on in my Flow experience which gave my simple mind much grief at the time (Flow Loop Doesn’t Start at First Record)!