In this post we will see how to Parse the Multi-Select Picklist Values in a Flow. Parsing the Multi-Select Picklist Selected values in a Flow is required where we need to use Selected Multi Select Picklist values
later inside the Flow for Loop and various use cases like as a Collection variable to Loop on them. We will Assign the Multi Select Picklist Values to a Collection Variable. We will also see Salesforce Flow Loop through Multi Select Picklist in this article. Best part is that you just need to download an Apex Action to use this Feature .
Access the Multi Select Picklist Values Individually in the Flows
Salesforce Flow Multi Select Picklist Parsing can be required on Screen Flows where we need to know and use values selected by user from a Multi Select Picklist on a Flow Screen and also on Auto Launched or Record Triggered Flows
where we need to use the existing values of a Multi-Select Picklist of a record within the Flow. We basically need to have a Salesforce Flow Multi Select Picklist Collection Variable so that it can be used
as a Flow Loop Variable to Loop on Multi Select Picklist Field values.
Why we need to Parse Multi Select Picklist Values in Flow?
Well, we need it because whenever we use a Multi Select Field on a Flow Screen or query a Multi select field value of any record using a Get Element, the format of the Multi Select values is like this [A;B;C;D] which are
semi-colon separated and not comma separated. This kind of variable is not considered as a Flow Collection Variable and cannot be used as a Flow Loop Variable. We somehow need to convert the Format of these values to
[A,B,C,D] similar to a Text collection variable so that it can be used in Loops to Loop on Multi Select Values in a Flow.
How to Parse Multi Select Picklist Values in a Flow?
There are many solutions that I found on Google for a workaround of the problem I mentioned earlier about the Multi Select Picklist Value output format.
But they all require either a Big Formula or lot of Flow Elements and overall the Flow becomes quite complex.
I have created a very simple Apex Action which can be easily downloaded and used in Flow to get the desired result and it creates a Text Collection Variable of the Multi Select Picklist Values. This collection variable can
then be used to Loop or to perform any kind of operation / logic later for the selected or existing Multi Select Field values.
Note : We will see how to use this Apex Action in Screen Flows or any other type of Flow.
Download the Un-Managed Package from Links Below
Note : In case Package throws issues while Installation, below is the basic code that can be copy pasted in the org and can be used instead of the package.
Apex Class
global class MultiSelectFlowValues {
@InvocableMethod
public static List<list<string>> CheckValues(List<string> values) {
if(!(values.isEmpty())){
string tempStr = values[0];
List<String> lstnew = tempStr.split(';');
list<list<string>> finalLst = new list<list<string>>();
finalLst.add(lstnew);
return finalLst;
}
else return null;
}
}
Test Class
@isTest
public class MultiSelectFlowValuesTestClass{
@isTest
static void MultiSelectFlowValuesTest(){
List<string> tempLst = new list<string>();
tempLst.add('AccidentalCoder');
tempLst.add('Salesforce');
MultiselectFlowvalues.CheckValues(tempLst);
}
}
IMPORTANT NOTE: IF THE CODE DOESN'T WORK FOR SOME REASON WITH MULTIPLE VALUES/IDS, REPLACE THE MAIN CODE WITH THE CODE BELOW AND TRY : THE ONLY CHANGE IS HIGHLIGHTED IN YELLOW. TEST CLASS REMAINS SAME.
global class MultiSelectFlowValues {
@InvocableMethod
public static List<list<string>> CheckValues(List<string> values) {
if(!(values.isEmpty())){
string tempStr = values[0];
List<String> lstnew = tempStr.split('; ');
list<list<string>> finalLst = new list<list<string>>();
finalLst.add(lstnew);
return finalLst;
}
else return null;
}
}
Steps to Use this Package/Component :
1. Install the Package (for all Users) from link above (This step not needed if you copy the code instead)
2. On the Flow Canvas, Go to Action Element and Drag it on the Flow Canvas.
3. Search for Multi Select Flow from Search Box in the Action Element.
4. Select the option MultiSelectFlowValues which is the name of the Apex Class. Once Package is downloaded this will be visible otherwise you wont see this option.
5. Provide a name of the Element.
6. Click on the Toggle Bar to include Input Values in the Action Element Screen.
7. In the Input value box just pass the variable which has Multi Select Picklist Values and need to be converted into a collection variable. This value can be passed from the Screen Multi Select Picklist component or from a
Record Field or any other variable that has stored multi select picklist value.
8. That's it.
Important : Now this Flow action Element will behave like a Text Collection Variable and can be used anywhere inside the Flow.
The name of this collection variable will be the name/label of the Action Element that you provided in step 5 above. Checkout how that works in this article later.
|
Become Flow Champion - Click & Start Learning
|
How will this work and what will it do?
This is a very simple, basically to this action just pass any variable which contains Multi Select Picklist values either from a Screen Input or a Record Field. Once you do it, the Action Element will itself behave as a Collection
Variable and can be used anywhere.
Assign Multi Select PickList Values to a Collection Variable in Flow
Basically it will take Multi Select Picklist values in format like this [A;B;C;D] and will return a text collection variable which actually looks like this [A,B,C,D].
Let's See this in action through a quick Example on a Screen Flow MultiSelect Picklist Component.
Multi Select Picklist Component on Screen Flow or Flow Screen
Let's say we have a simple Screen and it has Multi Select Pick List Input component added to that Screen and it displays a Case Field of type Picklist(Multi Select).
The name of this Multi Picklist component on Screen is say Case MultiSelect (Api Name : Case_MultiSelect). So, now whenever users will use this screen to populate this field the value of the Multi Select input will be
automatically stored in this screen component named Case_MultiSelect.
So now if you need to use this variable later in the Flow just pass this variable name into the Input box in our Action Component as mentioned in the Steps. And it will in turn return you the values in Text Variable Format which
can be used for any use later on in the Flow like using for Loop and all.
See Pics Below for better Clarity.
1. Add Multi Select Picklist on a Flow Screen and add a Multi Select Picklist Choice Set. I have added a Multi Select picklist choice set of case object and a multi-select field named Case Type.
The name is Important for this Component as all the user selected values are stored in this variable with the same name.
|
Added a Multi Select Picklist Component |
As you can see the values for this Multi Select Picklist will be stored in the Variable named Case_MultiSelect.
2. Action Element - Now if you want to use the values of this variable as a Collection Variable to Loop over, we just need to Pass this variable in the Action Element as per steps mentioned above in article.
Add the Action Element and search and choose the option MultiSelectFlowValues as per steps shown earlier.
|
Choose the Apex Action with name MultiSelectFlowValues |
3. Enter Name of the Action which will later behave as a Text Collection Variable. In our case I have named it as Case_Multi_Select as per image below.
|
Add the Name of Action which will behave as a Text Collection Variable |
4. Enter the Input Values and Pass the variable which contains the Multi Select Pick List Values. In our case, the Multi Select Pick List values are stored in Screen Component named
Case_MultiSelect which we created in Step 1
here. Just select and pass that to Action as per Images below.
So after saving this, Action Element box looks like this as shown below
5. Text Collection Variable : Now automatically this Action Element will behave as a Text Collection Variable which can be used for any kind of operation which can be done on a collection variable inside a Flow like using it
in a Loop.
In our case the collection variable name was Case_Multi_Select from last step which can now be used as a Text Collection Variable.
Salesforce Flow - Multi Select Pick List Loop
Let's see an example how can I use it to run a Loop on this Text Collection Variable (Case_Multi_Select). I will simply select this collection variable to Loop over the Pick List Values selected on screen.
Example Video : Here's a Quick Video showing the Apex Action's usage when you need to pass and use a Multi Select Field values from a Record from Salesforce. In this video I am fetching a Case Record and then passing the value of a Multi Select Picklist into my action and in return I get a Collection Variable that can be used for Loops etc.
Important : I have just shown how this component will work with Screen Multi Select Picklist values.
But similarly, it will work for any Multi select picklist values from any object or record's field on any type of Flow.
For example in Record Triggered Flow on Case, I will pass $Record.Case_Type__c into my Action Element to get a Collection Variable of the stored values.
Hope this Helps! Reach out to me here or on LinkedIn for any questions, issues or suggestions. Happy to Help!
IMPORTANT NOTE: IF THE CODE DOESN'T WORK FOR SOME REASON WITH MULTIPLE VALUES/IDS, REPLACE THE MAIN CODE WITH THE CODE BELOW AND TRY : THE ONLY CHANGE IS HIGHLIGHTED IN YELLOW. TEST CLASS REMAINS SAME.
global class MultiSelectFlowValues {
@InvocableMethod
public static List<list<string>> CheckValues(List<string> values) {
if(!(values.isEmpty())){
string tempStr = values[0];
List<String> lstnew = tempStr.split('; ');
list<list<string>> finalLst = new list<list<string>>();
finalLst.add(lstnew);
return finalLst;
}
else return null;
}
}
----------------------------------------------
Learn Flows : Want to Learn Flows from Experts? Complete a Guided Course. Checkout my Personal Favourites below and Invest in Learning
|
Learn Flows from Experts - Click Images to Checkout Courses |
|
---------------------------------------------- |
I'm curious about the test class. It calls the MultiselectFlowValues class, but doesn't do anything with the return. Shouldn't it compare the return to a known quantity ("AccidentalCoder,Salesforce") to ensure the class is returning what is expected?
ReplyDeleteHi Vince, you are absolutely right.
DeleteYou can enhance that test class but I just wrote a basic one just to get the coverage.
this is super awesomeeeeeeeeeeeeeeee
ReplyDeleteOMG !!!!!
Hi Vibhor, this is really helpful, thank you so much!
ReplyDeleteI'm still running into an issue and hope you can help me: We have two custom objects where we have a Multi-select picklist field with a country list. Let's say Object A and Object B where object B holds a list of "allowed" countries.
In the end, all countries in the field in Object A need to be removed from the Picklist, that are not in Object's B allowed countries. The list in Object B is not fix, so I need a cleaning flow that is triggered manually (with a link or button) (and it sets a date that from now on, countries of object A need to be checked against allowed list)
I'm using a flow for that purpose.
In fact, I have two flows. There is one flow triggered from object A if there are changes in the Multi-select Picklist field, that checks, if all countries are allowed. It compares the two lists with loops and your apex snippet and it is working fine!
But then I have another flow triggered from Object B. It finds all relevant object A records and updates them. And there I get an error message:
"Error Occurred: The number of results does not match the number of interviews that were executed in a single bulk execution request."
This error occurs at the first element in the flow, where I am creating a list of countries from the picklist. Do you have an idea what that means? And how to avoid it?
The strangest thing is: even if I get those error messages, the functionality is working fine.
Any idea?
Thanks
Henriette
Could it be a problem, that I am trying to update several records B at once? Or should that work?
DeleteSorry not able to understand the issue and also I haven't encountered this error before so cant suggest anything right now :(:( ..Need to maybe see the flow screenshot to understand better. You can message me on linkedIn or mail me at vibhor.3k@gmail.com
DeleteHi Vibhor,
Deletethank you for your reply.
Sorry, I think my explanation was not that easy to follow...:( in the end I could figure out that I always got an error when I did bulk updates that triggered this method for several records at once.
I also asked in salesforce community and got help :)
https://trailblazers.salesforce.com/answers?id=9064S000000DfawQAC
When I applied the changes to your class that Keiji suggested in the post linked above, it worked. I don't get the error message any more if I try to update records in bulk!
Happy to share the solution with you and anyone interested :)
Best wishes,
Henriette
This is the version he suggested and that worked for me:
global class MultiSelectFlowValues {
@InvocableMethod
public static List> CheckValues(List values) {
if(!(values.isEmpty())){
List> finalLst = new list>();
for (String tStr : values) {
List lstnew = tStr.split(';');
finalLst.add(lstnew);
}
return finalLst;
}
else return null;
}
}
Thanks a lot Henriette. This is a very good find and thanks for providing the solution for bulk records. For sure it will help many of us in future for this scenario
DeleteHi Vibhor ! Thank you for that flow component !
ReplyDeleteI am running into the following issue :
- I have created a screen flow and added a multipicklist that allows you to multi-select several records
- I have added the APEX action in the flow and set it to process the multipicklist
- I have created a loop related to the apex action
When I execute the flow and it reaches the loop element, it only works correctly for the first iteration, and fails at the second iteration (when it goes through the loop for the 2nd time). I get the following error message :
^ ERROR at Row:1:Column:185 invalid ID field: a1N1w000001NqO0EAK.
I don't know why it added a dot at the end of the record Id on the second loop iteration.
Is perhaps your apex action only meant to process multipicklists within the record itself (as opposed to multipicklists added in screen flows?)
Thank you a lot for your help !
Hi Coralie, I had similar error (i think), and I realised that collection variable stores IDs separated with ', ' (space behind comma), so I made a formula that substitutes space before ID (ID originaly looked like ( xxxxxx) instead of (xxxxxx).
DeleteThis comment has been removed by the author.
ReplyDeleteI love this idea! I am not sure if it will work for me but I am trying and maybe you can give me some advice.
ReplyDeleteOn the Opportunity object I have a Multi-Select Picklist called "Rooms of Interest." I am trying to create a screen flow that a user can use to find opportunities where the list includes the values selected. Example. I have selected a value or values (Dining Room, Living Room, Bedroom) at time of entry on many opportunities. Today, I want to find any where the opportunity had either Living or Dining selected.
I would like to display the list to the user, then fetch the matching records.
My goal would be to surface this flow on a home page for the user to initiate a search in lieu of a report (never fun with a MSP)
This blog post really helped me thank you so much!
ReplyDeleteThere is a very similar project on unofficialSF that you might consider joining. Solves the problem with a few more bells & whistles. See: https://unofficialsf.com/multiselect-magic-manage-multiselect-picklists-with-flow/
ReplyDeleteI have a slightly different use case, so before I attempt to use the Apex action I wanted to ask the question. I need to know if a specific Account is covered by a salesperson. The accounts are covered by State. Each salesperson's territory is a multi-select picklist. So the comparison I am trying to make is
ReplyDeleteDoes Account.State = Salesrep_Territory__c (multi-select picklist with states)?
In other words, does is the value of the state in the territory picklist for this rep? It seems like this should be done using a flow formula, but do I need to separate the values of the territory picklist into a text collection to make this comparison? Thanks for your insight!
There is a flow component that will let you inspect, add, remove and get the complete set of values of any multi-select picklist - saves hours of headaches...
Deletehttps://unofficialsf.com/multiselect-magic-manage-multiselect-picklists-with-flow/
IMPORTANT NOTE: IF THE CODE DOESN'T WORK FOR SOME REASON WITH MULTIPLE VALUES/IDS, REPLACE THE MAIN CODE WITH THE CODE BELOW AND TRY : THE ONLY CHANGE IS HIGHLIGHTED IN YELLOW. TEST CLASS REMAINS SAME.
ReplyDeleteglobal class MultiSelectFlowValues {
@InvocableMethod
public static List> CheckValues(List values) {
if(!(values.isEmpty())){
string tempStr = values[0];
List lstnew = tempStr.split('; ');
list> finalLst = new list>();
finalLst.add(lstnew);
return finalLst;
}
else return null;
}
}
Hi, ive been using the above^ code to parse multiple values from a multi-select picklist field for a few months now and it has worked like a charm. However, for some reason starting last night it now returns the original list with ";" instead of the "," separated list. I haven't updated anything on my side.
ReplyDeleteSee below:
MULTISELECTFLOWVALUES (APEX): parse 1
Inputs:
values = {!One} (a7F6T000001xsCPUAY;a7F6T000001xsCRUAY)
Outputs:
[a7F6T000001xsCPUAY;a7F6T000001xsCRUAY]
MULTISELECTFLOWVALUES (APEX): Two
Inputs:
values = {!Two} (a7F6T000001xsHAUAY;a7F6T000001xsHDUAY)
Outputs:
[a7F6T000001xsHAUAY;a7F6T000001xsHDUAY]
Any thoughts?
I started experiencing this as well. I think it has to do with the Winter Release possibly changing the way the multi-select values are returned in the flow. It seems like there's no longer a space after the semicolon in the list of values. Try updating the following line in the class:
DeleteFrom this:
List lstnew = tempStr.split('; ');
To this (note the space is removed):
List lstnew = tempStr.split(';');
This fixed it for me.
Chelsea's comment fixed the issue for me too, as of August '22.
DeleteHello,
ReplyDeleteI am getting this error: "Unfortunately, there was a problem. Please try again. If the problem continues, get in touch with your administrator with the error ID shown here and any other related details. Error ID: 203828799-868816 (-1298541144)" which doesn't tell me a lot. Has anyone else received this error? I've followed the steps outlined above.
I found a solution. I had to define a collection variable, and then in the apex action under advanced check off manually assign variables and set it to the collection variable I just defined.
DeleteThe Action has all of a sudden started to remove special signs and spaces from the values in the multiselect picklist. It used to work fine.
ReplyDeleteI use the action on the Opportunity object where I have a mulitple picklist from wich the salesreps selects customers that will be part of the deal.
Several customers (municipalities) will get the product, one will pay for them all. The price is
based on the total number of inhabitants.
I use the account names to look up the number of inhabitants from each municipalitys Account record.
The problem is that now I cant find the account records anymore because the action truncates the names.
Does anyone now of a way to get the values to remain exaclty as they are written in the picklist?
Example:
Values in the picklist field:
Demokunde: AVERØY KOMMUNE;Demokunde: KRISTIANSUND KOMMUNE
Current value written back to a textfield on the triggering record looks like this:DemokundeKRISTIANSUNDKOMMUNE
You have posted such a fabulous post; I am looking for such type of helpful posts. Thanks for sharing it. Visit my link as well. Msp Marketing Agency
ReplyDeleteThis has worked WONDERS for me. But I'm using it slightly differently in a new flow and getting an error. Instead of it parsing through the multi select picklist values from a screen the user was prompted with, I was hoping it'd do the same for a multi select picklist on the object I'm working with. I have a Get variable which gets the record and then for the it should Parse I have the specific field selected. So it's {!Get_Opportunity.Products_Churning__c}, but I'm getting an error saying "Internal Server Error" when trying to debug, any ideas?
ReplyDeleteAnother Anon mentioned earlier (and it worked for me) "I had to define a collection variable, and then in the apex action under advanced check off 'manually assign variables' and set it to the collection variable I just defined."
DeleteI need to use the collection in an update element but it does not show up when clicking into the values. I am updating the multiselect picklist with new values but I need to insert the existing values as well so I do not wipe out the old ones.
ReplyDeleteI didn't want to use Apex to make it easier for future developers troublesooting, so I build a Text formula field concatenating all the picked values in the multipicklist, and then in the flow using the 2 formulas, left and right in a loop, to reverse the process and add into the collection variable each left value. The only disadvantage is that the Text formula field uses an 'If-Includes the value add the value' combination that requires to keep it updated manually every time a new value is added to the multipicklist.
ReplyDeleteAlso doing it without Apex, is possible to use it in any edition I guess? Essentials and Professional.
ReplyDeleteIs it possible to adapt this functionality to a Record-Triggered Flow? I'm looking to obtain the chosen value of a custom Picklist field in the Account object.
ReplyDelete