Convert JSON List to Collection Variable in Salesforce Flow
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
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
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
With this InvocableMethod, basically you’re able to convert single variable to Collection Variable.
f. loop the CollectionOfColors variable
g. Add another MultipleJSONParser after the loop, this time you have to parse each JSON and parse each color, category, and type fields.
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
i. After all the steps your flow will look like this
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 :)