How to loop through multi-picklist record choice sets In Salesforce flows
- May 26, 2022
Requirement: We have two custom objects — Class and Student. Class and students have a lookup relationship where Class is Parent Object.
We want to create a button on Class Object that will give us all the students in a multi-select picklist who have Class field blank and once selected all the selected students should be added to that class as Related records.
Reason looping is required : The multi-select picklist component will return is as “Record1_ID;Record2_ID;Record3_ID;Record4_ID” as a text values and we will need to all these Ids in a collection to be able to use these Ids in our Flow.
Steps
- Create a New Screen Flow and create a Record ID variable that will store the ID of current record (Class)
API Name : recordId
DataType : Text
Available for Input = True
Note:- API name is case sensitive.
- Add a screen component then add a multi select pick-list component on your screen name it Student List.
- For choices create a Record choice set resource.
- API Name – Student_list_with_Class_blank
- Object – Student
- Conditions – Class is null Equals True
- Choice label – Name Field
- Data Type text
- Choice Value – id
- Create a Text Variable that will store the results from Multiselect Component (API Name : StudentIDS)
- Add an Assignment element in your Flow.
Label (API Name): Add_StudentIDS
StudentsIds Equal {!StudentList1}
StudentsIds Add ;
- Create two formulas and One Collection.
Formula 1: First_ID
DataType: Text
Syntax: TRIM(LEFT({!StudentIDS}, FIND(“;”,{!StudentIDS})-1))
Formula 2: Remove_FirstID
Data Type: Text
Syntax: TRIM(SUBSTITUTE({!StudentIDS},{!First_Id}+”;”,””))
Collection: StudentIDCollection
DataType: Text
Allow Multiple Values = True
- Add an Assignment element to the flow.
Label: AddIDstoCollection
Assignments
StudentIDCollection Add First_ID
StudentIds Equal Remove_FirstID
- Add Decision to the Flow
Label: StudentIDsContainSemiColon
First Outcome
Label: Yes
StdentIDS Contains ;
Rename Default Outcome to No
- For yes Outcome connect to AddIDstoCollection element For No add a Loop
Label: Loop_StudentIDCollection
Collection Variable: StudentIDCollection
Direction: First to Last Record
- For Each Item in the loop Add Get records
Label: GetStudentRecords
Object: Student
Condition
ID equals Current Item in the loop
(Store only first record)
- Add an Assignment element.
Label: Update_Student_Record
Assignment
{!GetStudentRecords.Class__c} Equal Record ID
- Create a record Collection Variable
Label: StudentList
Data Type: Record
Allow multiple values = True
- Add assignment Element
Label: AddStudenttocollection
Assignment
{!StudentList} Add Studentfrom{!GetStudentRecords}
- After the last record from Flow add update records element
Label: Update Students
Select: Use the IDs and all field values from a record or record collection
Record Collection: Student List
- Save & Activate the flow and create a button on Class Object to launch the flow.
Button on Class object
Label: Add Student
Detail Page button = True
Content Source :URl
URL: /flow/Add_Students?recordid={!Class__c.Id}?returl={!Class__c.Id}
Add the button to the page layout and you are all set.
— By Rishabh Dubey