Convert Selected Choices to Collection Variable in Flow

Convert Selected Choices to Collection Variable in Flow

Screen Flow is the only flow type that can be used to collect or display data in Salesforce. There are many input components that you can put on a screen. Besides the simple ones like text, number, or date, there are some input components that let the user select a value from the defined choices. Radio buttons, picklists, and checkbox groups can be used for this purpose. Unlike picklist fields that we know from the objects, these selection components can display records as choices too. So that it is possible to ask the user to select a record from a list. These selection components support multi-selection as well (multi-select picklist and checkbox group), which means that users can select more than one value from a list of choices. When the user selects only one record, then the screen component stores the value that you defined. So that you can use the selected value in your flow. However, when the user selects more than one choice, screen component stores all the selected values in a single variable. Since it is a variable of selected values separated with semicolon, it is not possible to loop through the selected choices, you have to convert it to a collection variable.

Read this article to learn more about the multi-select picklist screen component.

Convert Selected Choices to Text Collection

Even though flow doesn’t store the selected values in a collection variable, it is still possible to convert them to a collection variable.

Let’s assume that you have a screen that displays account records as choices. It displays account names as choice labels but stores the Ids as a text value.

account record choice set

When user selects records from this checkbox group, flow stores the selected record Ids in this format:
0011n00002BT1rLAAT;0010Y00000tDP7nQAG;0011n00002BSo1nAAD
Since it is just a single text value, it is not possible to loop through them. In order to convert it to a text collection, follow these steps.

1- Creating a Text Variable and Assigning Values

First of all, create a text variable called SelectedIds and use an assignment element to assign the selected records to this new variable. Pay attention that Select_Accounts is the name of the screen component. Using the same assignment element, add “;” to the same variable.

assign selected values in order to convert into a collection

At the end of this assignment, SelectedIds stores a value in this format:
0011n00002BT1rLAAT;0010Y00000tDP7nQAG;0011n00002BSo1nAAD;
As you can see, there is a semicolon at the end of each Id value. This will make it easier to extract the values since it follows a specific pattern.

2- Creating Formula Resources

In this step, you have to create two text formula resources.

  • FirstID
    TRIM(LEFT({!SelectedIds}, FIND(“;”,{!SelectedIds})-1))
FirstID formula

This formula extracts the first record Id from the left side of the SelectedIds variable.

  • RemoveFirstID
    TRIM(SUBSTITUTE({!SelectedIds},{!FirstID}+”;”,””))
Formula to remove the first Id

This formula removes the first Id value and its semicolon from the SelectedIds variable.

So at the end of these two formulas, you have the first Id value and the remaining values. Therefore, it is time to add the first value that you extracted to a collection.

3- Creating a Collection Variable and Assigning Values

Create a text collection variable and add another assignment element to add the FirstID into the collection. In the same assignment element, change the value of SelectedIds to the value of RemoveFirstID formula.

new text collection variable
Add the first Id to collection in order to convert the selected values into a collection

At the end of this step, you have a text collection that stores the first record’s Id and another text variable that stores the rest of the Ids with semicolon. You have to repeat this step in order to extract all of the values and add them into the collection.

4- Add a Decision to Check Remaining Values

In order to repeat step 3 for all the selected records, add a decision element that checks if there are remaining records in the SelectedIds variable. If there are, go back to the step 3.

Decision element to check the selectedids variable
Decision element on the canvas

The End

This is the end of the steps to convert the selected choices to a text collection variable. After this last step, you can continue your flow with any action that you want to perform. Since you have a collection now, you can iterate over the items.

Flow to convert selected values into a collection

Read this post to see a real example of a flow that uses these steps.

Creating an Autolaunched Flow To Reuse as Subflow

You can build an autolaunched flow that receives a text variable and converts it to a text collection variable using these steps. Then, whenever you want to convert the selected choices to a text collection variable, just call this autolaunched flow as a subflow. Pass the text variable as the input and get the text collection variable as the output. By doing this, you won’t need to perform these steps in every flow.

These are the changes that you need to make:

  • Create an autolaunched flow.
  • Edit SelectedIds text variable and make it available for input. To make it more generic, rename it as SelectedChoices. When calling this subflow, you will pass your text variable into this input variable.
SelectedChoiced text input variable.
  • In the first assignment element, remove the first line of the assignment that you did before, just add a semicolon at the end of this variable.
Add semicolon at the end of the text variable.
  • Edit the RecordIDCollection variable and make it available for output. Rename it as SelectedChoicesCollection, so that it will be more generic. This will be the output of this flow.
SelectedChoicesCollection variable
  • Rename the formula resources, so that they will be more generic as well.
    FirstID –> FirstChoice
    RemoveFirstID –> RemainingChoices

At the end, your autolaunched flow should look like this.

Autolaunched flow to convert multi-select picklist

Action Time

Let’s call this subflow from the undelete flow that you can see in this post.

Add the subflow element to the canvas, select this autolaunched flow, pass your text variable (in this case it is Deleted_Accounts) as an input. Manually assign the output value of the flow to your text collection variable (in this case it is FinalList).

Call the subflow to convert selected choices to text collection variable.

At the end, your flow will look like this. By reusing an autolaunched flow as a subflow, you won’t need to do these steps again.

Example to use the subflow.

Installation

If you don’t have time to build it or still unsure about the steps, you can install the autolaunched flow from this link.

2 Comments

  1. Thanks – this is exactly what I needed for my flow!
    A tip for other users – make sure to paste the Formulas in Step 2 as plain text – I didn’t and the formula initially didn’t save correctly because the quote marks had some kind of styling that interfered.

Leave a Reply

Your email address will not be published.


*