Flows | Loop on Checkbox Group Values in Flow using Apex Action

Salesforce Flow Get Values from Checkbox Group. In this post we will see how to Parse and use the values from a Multi Select Checkbox Group on a Screen Flow. Salesforce Flow Multi Select Checkbox can used for many use cases. We will use a Record Choice Set to show Multiple Checkboxes on Flow Screen and display Record values as choices or checkboxes. After that we will explore a way to use the selected values from the Record Choice Set to perform actions like Looping over the Checkbox Group values.  Let's see how to use the Salesforce Flow Checkbox Group Output effectively. 

The best part is all you need is a Re-Useable and downloadable Apex Action to Parse Multi select  CheckBox Group Values. 

Loop on Checkbox Group Values in Flow

Why we need to Parse Checkbox Group Choice values in Flow?

When we use a Checkbox Group on a Screen Flow, we are not able to use the user Selected values in Loops or Update Elements. This becomes an issue in many use cases where you need to Loop on Selected Values from a Check Box Group in Screen Flows. 

Whenever we use a Checkbox Group choice set on a Flow Screen, the format of the selected values is like this [A;B;C;D] which is a semi-colon separated  String Variable and not a Collection Variable. 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 selected values to [A,B,C,D] similar to a Text collection variable so that it can be used in Loops to Loop on CheckBox Group Selected Choices or Values in a Flow.

How to use the values of Choice from CheckBox Group in Flow?

We will just use an Apex Action which can be downloaded and used to use the values. 
Checkout this Article where I have shown the same functionality but instead used Multi Select Picklist values to use in Loops Use Multi Select Picklist Values in Loops and Other Operations . Exact same Apex Action can be used in similar way for Checkbox Group in Screen Flows.



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;

   }

}


Learn Salesforce Development from Scratch


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.
7In the Input value box just pass the Screen variable name which has Checkbox Group Values and need to be converted into a collection variable. 
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.

Loop on Selected Values of a Checkbox Group in a Flow

When you use this Apex Component and Pass selected Record Choice Values or checkbox groups values to it, it converts those selected values into a Collection String variable which can be used to Loop on selected values of the Record Choice or Checkbox group.

Let's see this with a simple example below in a Video.

Now when a User will select some values from this screen, we will use the Action to get a String Collection Variable of these values which can be used for Looping on these selected Records.

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;

   }

}


There are many more use cases but I am sample Use Cases to Loop over some Selected Records from a Checkbox Group. Below Video shows a sample Use Case where we will show some Case records on the Flow Screen using Checkbox Group.

Checkout this Article where I have shown the same functionality but have Parsed on Multi Select Picklist values to use in Loops Use Multi Select Picklist Values in Loops and Other Operations . 

BEST ONLINE SALESFORCE COURSES - START LEARNING TODAY!
If you like reading my content, Subscribe to my Upcoming YOUTUBE Channel here: https://www.youtube.com/channel/UCdfi8Sq7iOcxnFhsgulWHeg/videos?view=0&sort=p&flow=grid

Don't Miss Latest Posts :  CLICK HERE TO SUBSCRIBE TO THIS BLOG

Checkout All Flow Examples on this Blog: All Flow Examples

Get Sharing and Visibility Designer Certification

Learn Flows Online

Comments

  1. Hey so whenever I try to run an update in the loop, it works for one record and second gets an error though: Error Occurred: This error occurred when the flow tried to update records: SELECT Id FROM Account WHERE ((Id = ' 0010n00001C35FOAAZ')) LIMIT ^ ERROR at Row:1:Column:32 invalid ID field: 0010n00001C35FOAAZ . Any Idea what's the issue with that? That seems like a valid 18-digit ID and I can pull it up in the UI fine.

    ReplyDelete
    Replies
    1. Figured it out, the list saves with spaces and results in faulty IDs. Running them through a TRIM() formula prior to updates resolved the issue.

      Delete
    2. Hey, I'm nor an Apex programmer, however I'd like to know what part of your code (or if you can repost) your code to fix the issue of the space before the Id, as the previous poster noted. Your solution is elegant however it only works for the first Id...
      Good job btw!

      Delete
  2. 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 . TEST CLASS REMAINS SAME.

    global 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;

    }

    }

    ReplyDelete
  3. This is fantastic. I'm wondering if it would solve my issue of adding existing child objects to a master via Checkbox Group? I make the selections but it always updates all records versus the ones I selected. I installed your Apex action but can't figure out how to move it from text back to records to use for updating. https://trailhead.salesforce.com/trailblazer-community/feed?tab=my_activity&sort=LAST_MODIFIED_DATE_DESC

    ReplyDelete
  4. Trying to figure out why this Apex action blows up my flow. Dowloaded and installed the action just fine, but now I can't debug or execute my flow without getting an internal server error. Error goes away if I remove the Apex action from the flow.

    ReplyDelete
    Replies
    1. Basically, as soon as I add the Loop using the {!Parse_Checkbox_Group} collection variable the flow throws an error. So it's not the Apex action itself but when I try to use the collection variable in a loop that is blowing something up.

      Delete
    2. What I discovered: I can assign the collection variable created by the Apex action to another collection variable and use that in a loop and it works fine. But if I try to use the original collection variable in the loop it doesn't work. So I have a workaround but it seems strange to me.

      Delete
  5. Thank you for creating this! This helped me out just when I needed it!

    ReplyDelete
  6. Hi , I have a senario like using a flow and ask users to add their family member details via screen, tried using loop but loop has to be based on till they want to add another new family member record (Checkbox). how to achieve this? tried loop or decision box still not able to conclude. can u advise?

    ReplyDelete
  7. Your every blog is too good for us. I love you blogging style. I hope in future you share more good blog with us. Now it's time to avail Baby Liquid Soap for more information.

    ReplyDelete
  8. @Vibhor, I got the flow to create multiple records, but how would I get the multi picklist to come up as a single field in each record?

    ReplyDelete

Post a Comment

Thanks for your Feedback!

PLATFORM APP BUILDER CERTIFICATION - 78% OFF

POPULAR POSTS

Salesforce Flow Examples : Flow Use Cases and Scenarios

Flows | Use Flows to Bulk Update Records from List View in Salesforce

Flow Bulkification | Mass Update Records from Flows in Salesforce


Never Miss Latest Posts