Creating or Updating Multiple Records in Salesforce Using Flow
One of the important and complex part of Salesforce flows is loops. Its a powerful concept that you can take the for loop concept in coding and turn it into something fun, visual and declarative without writing a line of code. One of the concepts that I like to touch in this article is the concept of record variables - single record variable and multiple record collection variable to build powerful flows to create or update multiple records. As always, it is more powerful when you have a use case to demonstrate with.
Creating Multiple Records Using Salesforce Flow:
Use Case 1: Lets say you are a university and you enroll students. You want to track their monthly progress. You are storing the monthly records in an object. You create these records at the beginning of the school year and the process is that when a student is marked as enrolled, you want the system to create these monthly records for the student.
In this use case, you have an object called Monthly Period. You have your standard contact record and then you have the Type field with a picklist value of Student and a dependent picklist called Enrollment Status. If the type = Student and enrollment status = Enrolled, then we want our flow to insert (auto-create) Monthly Progress (another object) for each Monthly Period record.
This is my flow that achieves this:
I am not going break down this flow element by element. Where I would like to focus on the Assignment part inside the For loop and then the single record variable and my collection variable which ultimately does the trick to add all the records.
Once I get all the records for Monthly periods, I loop through each of them. For each of the monthly status records returned, I need to make sure to create Monthly Progress records and relate to the contact, our student who's student.
Here I'd create a single record variable which looks like this:
The collection variable which I used in the last step of the assignment looks like this:
Then I use the single record and collection variable in the assignment element:
Note that I have used some sort of naming convention for my variables. Always come up with a standard so that your admin knows easily what's what by just looking at the variable name.
You will notice that within the same assignment element, the last row you will see varMonthlyProgressCollection. This is my collection variable which collects all the single records we want added in the loop. So thats why the operator is Add.
Recommended by LinkedIn
Once this is done, you have the last step to insert (create) all these records in the collection variable.
Boom, you are done! Test using the Debug button on flow to see if your flow is working as expect. Activate and test with actual records! :)
Use Case 2:
Similar to use case 1 and I won't go into as much detail but this was an actual ask from a client. Opportunity object is private and so are quotes. So, when an opportunity team member is added to an opportunity, share all the related quotes with the newly added opportunity team member. So, you need single record and collection variables to do the trick.
Update Multiple Records Using Salesforce Flow
Use Case: When an account is marked as Inactive, we want to update all the open opportunities as Closed Lost.
Here since we are not creating new records, we don't need to create a single record variable. Instead you'd use the current item in loop to update the opportunity stage name.
Finally, you update the items in the collection variable.
So, this is how we'd leverage the power of variables - single and collection in flow using loops to create or update records. Hope you enjoyed it and thinking of how you can use this in your projects.
Alekhya Mandadi Interesting article, I wonder how the get element in the first example looks like as you want to retrieve all the selected records from the list view.
Alekhya Mandadi Loved the step-by-step explanation and the real world usecases! Loops surely are super powerful! Collection variables definitely helps to keep Data elements outside the loop.. most recommended 😊