How to create a Map collection in Flows? – Part 1
Hola! Glad to see you here 😀
In this article, we are going to discuss how we can create Map collection in Flows? Excited already? Yes, I knew it. I will be splitting this topic in a series of posts to keep it light.
What is a Map collection?
If you’re a Salesforce Admin, then you may not know what exactly a Map collection is. By definition, a Map is an object that maps keys to values, or in other words, is a collection of attribute(also referred to as ‘key’)-value pairs. To give you an idea, you can think of the following as a map.
a:100b:200c:300d:400
So in this collection, each element is represented by a key-value pair. So for key ‘a’, the mapped value is 100, for ‘b’ its 200 and so on. This should give you a basic idea of what a map collection is. Map collection is heavily used in programming languages(including Apex), to help reduce the complexity of the code.
But do we really need it in flows?
The answer really depends on the business logic that you are trying to implement using flows. For some of the complex logics, you might find that having a map collection can actually help you in saving some SOQL queries and making it easier to implement.
How to create the Map?
The answer is Apex-Defined variables. I have listed a couple of resources towards the post’s end where you can read more about the Apex-Defined variables. For the demo purpose, we are going to create a map of Account IDs with their names.
Step 1: Create a class to define Apex Defined data type. For example:
public class FlowMapClass { @AuraEnabled public string key; //Map key @AuraEnabled public string value; //Map value public FlowMapClass(){}}
Step 2: Create one collection variable and one normal variable of Apex-Defined variable of the FlowMapClass type.
Step 3: Get the Account records using Get Records element.
Step 4: Loop over the Account records and store the value Account Id and name in the ‘key’ and ‘value’ field of the Apex-Defined variable using an Assignment element.
Step 5: Add the Apex-Defined variable to the Apex-Defined collection variable using an Assignment element.
And that’s it! You just created a map collection flows! 😀
Things to know!
Instead of looping over the accounts you can also create an Apex Invocable method and pass the collection of account records which returns a collection type variable of Apex-Defined data type. But remember, you cannot create a ‘generic’ method which accepts a collection and returns a map, because sObject list type is not yet supported in Invocable Methods. Creating the Invocable Method would help against the Max Element Executed limit.
The map obtained by this method will not inherit the properties of a standard Map collection, for example maps do not contain duplicate keys. We are only creating a collection which can be used as a good substitute for Map collection. So choose the ‘key’ for your map wisely.
How to get a value from the Map for a given key?
Given, you already have an Apex-Defined collection variable and a key.
Step 1: Create an Apex-Defined variable(this variable will be used as the loop variable).
Step 2: Loop over the Apex-Defined collection. And inside the loop, check if the value of the key of the Apex-Defined loop variable is equal to the given key. If yes, copy the corresponding ‘value’ of the Apex-Defined loop variable in a variable of appropriate data type using Assignment element. If no, go to next iteration.
Things to know!
Here if you try to create an Apex Invocable method to the get value from the map for a particular key by passing the Apex-Defined collection and ‘key’ as parameters , it will not work. This is something I have been working on and appears to be a flow design limitation/bug/issue for Apex-Defined variables. Still a work in progress for me, will share the updates if I can find any workarounds.
Overall Analysis
If you are going down this approach, it is very likely that you will hit the Maximum Flow Element Execute limit i.e. 2000. In my future posts, I will share some upgrades to this solution(*spoiler alert* there is a lot good stuff coming!). Also, I will be sharing few workarounds for how you can ‘cheat’ this limit in flows. Feel free to subscribe if you don’t want to miss the update. Also upvoting this idea: https://success.salesforce.com/ideaView?id=08730000000Dre0AAC
Resources:
Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

.png)




Comments