Salesforce Flow Loops (Collection Variable) – Best Practices and Example
In Salesforce, when working with flows, you can achieve iteration similar to how you iterate over sObject records in a trigger. This is done using a collection loop variable within the flow. Just like in a trigger where you store records in a list and iterate over them, in a flow, you can handle collections of records with ease, often in just a few clicks. This is one of the advantages of using flows over triggers – the simplicity and visual interface.
Consider a specific scenario: Suppose cases are logged within Salesforce, and an admin at Enceptor Solutions needs to identify cases created last week that remain open without a customer solution provided. These cases require escalation, where the admin needs to update their status from ‘New’ or ‘Working’ to ‘Escalated’. This ensures that a designated team can address them promptly, aligning with the business’s defined policy that cases should not remain in the ‘Working’ or ‘New’ status for more than seven days.
Alright, let’s begin like this: we’ll set up a scheduled flow to run weekly, precisely at 12:30 AM every Monday. This flow will retrieve records from the past seven days with a status of ‘New’ or ‘Working’, determined by their creation date.

Once you’ve configured the scheduler to run at a specific interval, the next step involves querying the records based on certain conditions. In our use case scenario, we need to retrieve cases where the creation date is less than today’s date (the flow execution date) and greater than or equal to seven days ago (Flow Current Date – 7). This ensures that only records from the previous week, spanning seven days, are retrieved.

Now you might be curious about the values like “CurrentDate” or “currentMinus7” and where they originate from. Don’t worry, they are simply part of the formula resource we created to execute our WHERE clause. Current Date is the {!$Flow.CurrentDate}.

When crafting a query, it’s crucial to be mindful of the fields selected to store in variables. Many developers overlook this step, potentially impacting performance and storage size. It’s essential to include only the necessary fields in the query result, rather than fetching the entire sObject. Below is an example illustrating this principle.

In this instance, we’re opting to store only one field – the Status field – as it’s the only one needed for the update. However, I’ve also included the Case Number for supplementary logic. Feel free to omit the Case Number if it’s not needed for your purposes. Id Field is selected by default, so you don’t need to worry about that.
Now, let’s proceed to the next interesting section of the flow: Iteration/Loop. With the list of records in hand, our task is to go through each one individually to assign the status. In this stage, there’s no need to include a decision node since we’ve already filtered the records above.

In Apex, we utilize a for loop with for each item. Similarly, within our flow, we employ the “Iterate_on_past_7_days_records” node element for each record. With each iteration, we update the status to ‘Escalated’ using the assignment operator.

This how it will look like when you click on variable to select it

Here you can assign multiple field values which you want to update as shown above.
Now, the assignment of value is completed with the updated values, it’s important to note that the changes haven’t been committed to the database yet. This is because we’ve performed assignment operations here, not DML operations, knowing that DML cannot be executed inside flows. Similar to how we create a List<sObject> in Apex to perform DML, here in the flow, we create a collection and store each item using the ‘Add’ function.
Now, we must establish a resource variable capable of accepting multiple records, enabling it to store the collection.

Next, we’ll set up another assignment within the loop, positioned beneath the previous one. In this assignment, we’ll utilize the “add” function to append each record to the newly created resource.

The final step involves executing DML on the stored collection, ensuring that the records will be committed to the database. Once this is done, you have successfully completed the task. Cheers!!

This is how we’ve learned to effectively utilize the flow collection variable and loop, optimizing processing time for greater efficiency.
That’s all there is to it! Your flow is now finished. The next step is to test it, although we won’t cover that in this article. Your finalized flow should resemble the following:
