Convert JSON List to Collection Variable in Salesforce Flow

Munawirrahman
4 min readDec 13, 2020

--

For you, who still haven’t read my article about JSON parsing in Salesforce Flow, I suggest you to visit this article, before reading this one.

As promised in my previous writing, this time I’ll show you how to process a JSON list and parsing it with Salesforce Flow without having to hardcode the list index in Salesforce Flow. And you’ll be able to loop the list in flow.

a. Install this InvocableMethod Apex Class to convert JSON list to collection variable here (don’t forget to change login to test.login if you want to install in sandbox):
source code: https://github.com/munawirrahman/JSONToCollection

b. For this article, I’ll use this JSON as example for parsing

{
"colors": [
{
"color": "black",
"category": "hue",
"type": "primary"
},
{
"color": "red",
"category": "hue",
"type": "primary"
},
{
"color": "blue",
"category": "hue",
"type": "primary"
},
{
"color": "yellow",
"category": "hue",
"type": "primary"
},
{
"color": "green",
"category": "hue",
"type": "secondary"
}
]
}

c. For the sake of example, I create a text template variable and put the JSON in point B in that variable. But you can use text variable as well for the parsing

Image 1: JSON example in Text Template variable

d. Use the action that I mentioned in my previous writing (MultipleJSONParser) and parse the colors field from JSON variable and I store it to ColorsObj text variable

Image 2: MultipleJSONParser to parse colors field
Image 3: ColorsObj Text Variable to Store Colors field JSON
Image 4: Store Field01 result to ColorsObj variable
Image 5: Result when I tried to debug

From image 5, you can see that we’re able to parse Colors field to Text Variable. But the problem is, this variable is not a collection variable, so we won’t be able to loop it and parse type, category, and color fields.

e. This is where, you need to convert that ColorsObj variable to collection of text. This is how

Image 6: Create a Collection Text Variable to store the converted value
Image 7: add Invocable action that you installed in point a. and add ColorsObjs as input variable, and store it to CollectionOfColors (collection text variable that we created in Image 6)

With this InvocableMethod, basically you’re able to convert single variable to Collection Variable.

f. loop the CollectionOfColors variable

Image 8: Loop ColectionOfColors variable

g. Add another MultipleJSONParser after the loop, this time you have to parse each JSON and parse each color, category, and type fields.

Image 9: Parse the each JSON

h. For the sake of demo, I’ll add each parsed result to one collection variables. But at this point, you can assign it to record or any logic you want

Image 10: Assign parsed result to Collection Variables

i. After all the steps your flow will look like this

Image 11: Final Flow
Image 12: Final debug result

Thing to consider:
This InvocableMethod only supports 5 converter in one Apex Action Element, feel free to add the numbers if you need to

There you go with this InvocableAction and this , you will be able to Parse JSON inside Flow and don’t need to add a new apex codes everytime you want to parse JSON

With this JSON Parser method, I am able todo many things you maybe didn’t think it’s possible with Salesforce Flow before. In the next writings maybe I’ll give you some use case how I utilize this Invocable Action :)

Please clap if you think this is useful, and comment if you have trouble when utilizing this action, I’ll try to help the best as I can :)

--

--

Munawirrahman
Munawirrahman

Written by Munawirrahman

A dots connector. 22x Salesforce Certified. Open for consultation, Email: munawirrahman@gmail.com

Responses (3)