Author: Rishabh Dubey
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:
1. 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.
2. Add a screen component then add a Multi select pick-list component on your screen name it Student List.
3. For choices create a Record choice set resource.
a. API Name – Student_list_with_Class_blank
b. Object – Student
c. Conditions – Class is null Equals True
d. Choice label – Name Field
e. Data Type text
f. Choice Value – id
4. Create a Text Variable that will store the results from Multiselect Component (API Name : StudentIDS)
5. Add an Assignment element in your Flow.
Label (API Name): Add_StudentIDS
StudentsIds Equal {!StudentList1}
StudentsIds Add ;
6. 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
7. Add an Assignment element to the flow.
Label: AddIDstoCollection
Assignments
StudentIDCollection Add First_ID
StudentIds Equal Remove_FirstID
8. Add Decision to the Flow
Label: StudentIDsContainSemiColon
First Outcome
Label: Yes
StdentIDS Contains ;
Rename Default Outcome to No
9. For yes Outcome connect to AddIDstoCollection element For No add a Loop
Label: Loop_StudentIDCollection
Collection Variable: StudentIDCollection
Direction: First to Last Record
10. 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)
11. Add an Assignment element.
Label: Update_Student_Record
Assignment
{!GetStudentRecords.Class__c} Equal Record ID
12. Create a record Collection Variable
Label: StudentList
Data Type: Record
Allow multiple values = True
13. Add assignment Element
Label: AddStudenttocollection
Assignment
{!StudentList} Add Studentfrom{!GetStudentRecords}
14. 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
15. 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.