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:100
b:200
c:300
d: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. 🙂

11 thoughts on “How to create a Map collection in Flows? – Part 1

  1. Pingback: How to create a Map collection in Flows by Narender Singh – UnofficialSF

  2. Pingback: How to create a Map collection in Flows? – Part 2(Flow map methods) #Spring20Delight |  forcePanda

  3. The reason you typically use a “Map” data-structure is due to the performance benefit of having a O(1) for retrieving any item in the collection, via the key.

    This implementation has a O(n) lookup time, and really doesn’t provide any benefit that I can see over just a List of Objects containing the same key & value information.

    Like

    • Hi,
      Performance is not really the point in this case. It’s more about how you can pass/store information in key-value format. And then you can extend the functionality by writing some invocable methods. 🙂

      Like

      • I agreed with @Chagonkas, this is not a good example for showing how to use key/value and extend the functionality of writing invocable methods. The reason of using Map is all about performance. If you show example of map usage in wrong way, others will pick it up and use Map but loop through all value in the map ( where I have seen that case ), this market will then be flooded with really bad developers. Please update the post with a focus, if you want to show how to use Invocable Method, show it but stay focus on Invocable Method itself and make it right. If you want to show how to use Map, do not use it in Invocable Method because true value of using Map in Flows is still not supported yet. Show it in a hello world method is better to let learns focus on what they need to learn.

        Like

      • Agreed. This was not really to show how to use Invocable methods. It was just to how one can utilize something like this if they’re not a hardcore dev.

        Like

  4. Narender Hi,
    Thank you for the post!
    Is there a way to perform the other way around and pass through an Invocable Apex Action a Map Collection and receive a Record Variable?
    Are you familiar with a way of converting the Map Collection to a Record variable within the Flow?

    Thanks!
    Gidi

    Like

    • Hi Gidi,
      You could do pretty much anything in Apex, but the question is how will you pass a ‘map’ from flow to Apex action? Because you can’t create a ‘map’ type variable in Flow. Apex defined types allows you to create something similar but that’s classified as a user defined data type and not the actual ‘map’ datatype.
      Makes sense?

      Like

  5. Hi Narender,
    Actually, I have a string with something like this: “First Name”:”John”, “Last Name”: “Smith”.
    This string might change frequently, so I was looking for a dynamic way to “convert it into a Record Variable, but couldn’t find a solution in Flow.

    Are you familiar with a way of performing it within a flow?
    If not, can I pass it through an Apex Action so the could will “convert” it into a Record Variable and pass it back to me? I thought about sending the string along with the Object API name through the action. Does apex will have to perform some kind of internal mapping for every Object I pass it, or it can use whatever I’m sending dynamically?
    I don’t want to add a code line for every “new” object I’m passing.

    Thanks!
    – Gidi

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.