How to create multiple junction object record using Flows in Salesforce?

How to create multiple junction object record using Flows in Salesforce?

We can use for loop within another for loop to iterate two or more list(Collection) variables and create junction object records.
Check the below sample implementation.
Objects:
Employee__c
Hobby__c
Employee_Hobby__c(Junction Object)
Flow Variables:
 
objJuncRec – Variable to hold junction object record.


listEmployees – Holds list of Employee records.

listHobbies – Holds the list of Hobby records.

listJunctionRecs – Holds the junction object records.

Flow:

Fetch Employees:
Fetches the needed Employee records. It stores the fetched records in listEmployees variable.

Fetch Hobbies:
Fetches the needed Hobby records. It stores the fetched records in listHobbies variable.

Hobbies Loop:
Iterates the Hobby records.

Employee Loop:
Iterates the Employee records.

Assign Junction Object Rec:
Assign values to objJuncRec variable. The master detail fields will get record ids from both the loops.

Adding Junc Rec to List Recs
Adds the objJuncRec variable to the listJunctionRecs variable.

Create Junction Object Records
Inserts the records in listJunctionRecs variable.

Leave a Reply