Loop and Assignment Element

In this blog you will learn about two very important elements of flow which is LOOP ELEMENT and ASSIGNMENT ELEMENT.
This is going to be a bit tricky example but if you will understand it properly, trust me you are going to ace any flow like a pro.
So are we ready?

Before going forward in this example and use case first let's learn about Loop and Assignment.

What is Loop?

In salesforce flow loop work same as it works in Apex or any other programming language.
If you want to go through multiple records you can do it by loop.
In apex if we want to loop through records what we do is, We get those records by Select Query and then loop through it. 
Same as, in flow we will use Get Records Element to get those records and then we can use the loop element and without writing any line of code we can loop through those records.

What is Assignment?

What do you understand by assignment? If we want to give a value to a variable, that means we are assigning value the value to the variable. Same as in flow if you want to assign any value to a variable/record variable you can do it with assignment element.

Now let's work on the use case.
Use Case : Our customer wants the functionality to check if all the accounts in their org have contacts related to them. If not, they want to create a contact for those accounts.

How we will achieve that?
  1. Create a flow where we will get all the accounts from the org
  2. Loop through the accounts
  3. Use get record element inside loop to check if there is any contact related to every account
  4. Take a Decision element to check if contact exsist in the get record element.
  5. Create a Record Variable for Contact Object
  6. Create a Record Collection Variable to add all the new contact one by one to the variable. Why this? because we do not want to use DML inside loop.
  7. Use Assignment Element to assign values in the record variable.
  8. Take another assignment element to assign the record variable to the record collection variable. Why? because the record variable will be overwrite when the loop will occur.
  9. After the last item in loop we will use Create Record Element to create all the contact record that we have saved in the record collection variable.
Now Let me show you all the steps with screenshots :
1. Get record element for getting all the accounts.

2. Loop through Accounts :

3. Get record element to get contacts related to account :

4. Decision Element to check if get record element has any contact record :

5. Record Variable for Contact object :

6. Record Collection Variable to save the record variable values :

7. Use Assignment element to Assign values to record variable :

8. Use Assignment Element to Assign record variable to Record Collection Variable :

9. Create Record Element to create all the contact record that we have saved in the record collection variable :

Here is the full tutorial video link :

If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Let's learn and grow together.
Love and Peace! 🧡

Post a Comment