Loop Implementation in Salesforce Flow

Flow Builder – Salesforce Configuration 

Flow Builder is one of the automation tools available in Salesforce. It collects data and performs actions in Salesforce org or an external system. This helps to build complex business solutions with clicks and not with code. It has multiple features including record triggered flow, execution of business logic using prior value, iterating over a collection of data, and getting input from user and so on. 

Loop Implementation 

The loop element in the flow iterates over the collection variable which is the list of records in which an action needs to be performed. It will loop through each record in the collection and stores the current item temporarily in the loop variable. 

Once the loop implementation completes the desired logic in the first record, then the flow will place the next record in the loop variable. Hence, to track the changes made inside each iteration, a variable need to be created. Then, the value in the loop variable needs to be assigned to it. Once the loop finishes all its iterations, the desired actions will be performed with the help of the tracked value stored in the discrete variable. 

Example 

Let us consider there is a business requirement to calculate the total amount of the debit and credit transactions performed by each customer. Without much effort, it can be done using Rollup Summary if the objects are in Master Detail Relationship. Unfortunately, the Transactions object reached its Master Detail limit and has been created with Lookup Relationship with Customer object. Here comes the flow builder that paved the way to address this requirement using the loop implementation. 

Step 1:  Create a Record – Triggered Flow and then choose Transaction as the object. Also, choose the options needed to execute the logic in both insertion and modification of the amount in the transaction records. 

Ensure that the transaction records associated to the customer is selected and then choose the option to execute the flow once the Transaction records are saved. 

Step 2: Using Get Records element, retrieve the successful transaction records associated with the customer record in which the total of Debit and Credit amount needs to be displayed using the below conditions. 

Create a Record Collection variable named getCustomerTransactions with Allow multiple values (collection) check box enabled to hold multiple records. 

Choose All Records option in How Many Records to Store section to store all the records retrieved from Get Records element. The getCustomerTransactions variable is used to hold all the retrieved transaction records with fields specified. 

Step 3: Create a Loop named Loop through Transactions and select the collection variable getCustomerTransactions to iterate over each transaction record stored in it. There are options to select how the loop should iterate over the collection. i.e., either starting from the First item to last item or from the Last item to first item. Here, we will proceed to execute the logic from First item to last item.  

Step 4: Inside the For Each flow path, add the Decision element to check the transaction mode and then create separate outcomes for Debit and Credit transactions. The transaction mode for the record can be checked using the API name of the Loop element Current Item from Loop Loop_through_Transactions. If the value is Debit, Debit outcome will be executed and if it is Credit, Credit outcome will be executed.  

Step 5: Create two different Assignment elements to calculate the total of debit and credit amount individually. First two currency variables need to be created to hold the value of the current item in the loop. SumofDebitAmount and SumofCreditAmount are the currency variables created to hold the sum of Debit and Credit value, respectively. These variables are necessary to hold the items in each iteration and performs calculation. In absence of these variable, only the value in the last record of the loop is maintained in the current item which will lead to inaccurate calculations.  

Then, the Transaction Amount in the current item of the loop tracked in Current Item from Loop Loop_through_Transactions is assigned to the respective variables of the assignment element. Here, the Add operator must be used to add the value in the current item.  

During the first iteration, it will add the amount in transaction record that has transaction mode as Debit and assign the summed-up value to SumofDebitAmount variable. In the next iteration, the value stored in SumofDebitAmount variable is summed up with the amount available in the current transaction record that has the transaction mode as Debit.  

Likewise, the amount value is calculated until the loop finishes the iteration of all the transaction records and the final total value will be stored in the SumofDebitAmount variable. Drag the path back to the start of the loop to execute the loop again until it completes its iteration. 

Similarly, the amount available in each transaction with transaction mode as Credit is summed up and stored in the SumofCreditAmount variable in the respective Assignment element. This is the entire view of the loop element that calculates the Debit and Credit Total amount.  

Step 6: After Last path, add the Update Records element to update the calculated values on the respective customer records. The action to perform should be done only after all the iterations in the loop is completed as it is a best practice to avoid multiple database operations. 

The Update Total Amount for Customers action will update the value that is stored in the SumofDebitAmount variable to Total Amount Debited and SumofCreditAmount variable to Total Amount Credited on the customer record.  

Step 7: Save and Activate the Flow. 

Below is the list of transaction records that are associated with the customer named Maria Garcia.  

The loop element calculated the total amount based on the transaction mode (Debit and Credit) for all the successful transactions and stored the total value in Total Amount Debited and Total Amount Credited value respectively in the Maria Garcia customer record. 

Summary 

Thus, the loop element in flow simplifies the process of calculations in a collection of records. Also, the loop reduces the time and effort in automating many complicated businesses use cases that involve repetitive tasks in large volume of data. 

Reference Links 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.