How to Loop Through Data in a Workflow
When You Need Loops
Most workflows process one item at a time: one form submission, one webhook event, one order. But some automations need to handle batches of data. A nightly report workflow might need to process every order from that day. A data cleanup workflow might need to check every contact in a list for invalid phone numbers. A content workflow might need to publish a post to every social media account.
Without loops, you would need to know the exact number of items in advance and create that many identical workflow steps. A loop handles any number of items with a single set of steps, whether the list has 3 items or 3,000.
How Loops Work in a Workflow
A loop node takes a list (an array of items) as input and runs the downstream steps once for each item in the list. On each iteration, the current item is available as a workflow variable. When all items have been processed, the workflow continues to the next step after the loop.
For example, if your loop input is a list of 10 customer records, the steps inside the loop run 10 times. On the first iteration, the current item variable contains the first customer record. On the second iteration, it contains the second record, and so on.
Step-by-Step Setup
Before the loop, you need a step that produces a list. This might be a database query that returns multiple records, an API call that returns an array of results, or a CSV file that has been parsed into rows. The output should be an array where each element represents one item to process.
In the Chain Commands visual builder, add a loop node after the step that produces the list. Configure it to iterate over the list variable. The loop node will execute its child steps once per item in the list.
Add the processing steps that should run for each item. These steps can reference the current item variable to access fields like name, email, amount, or whatever data each item contains. Keep the processing simple, as these steps execute for every single item.
If one item fails, you probably want the loop to continue processing the remaining items. Add error handling inside the loop so a failure on item 5 does not stop items 6 through 100 from being processed. Log failed items for later review.
After the loop finishes all iterations, you might want a summary step. This could count how many items were processed successfully, how many failed, and send a completion report. Connect these steps after the loop node's "done" output.
Practical Loop Examples
Batch Email Sending
Query your contact list to get all subscribers who signed up in the past week. Loop through each contact and send a personalized welcome email using their name and signup source. The loop handles 10 or 10,000 contacts identically.
Data Enrichment
Pull a list of leads from your database that are missing company information. Loop through each lead, call an enrichment API with their email domain, and update the database record with the returned company data. Each iteration processes one lead independently.
Multi-Record Updates
After a pricing change, you need to update every product in your catalog. Query all products, loop through each one, calculate the new price, and write the updated price back to the database. One workflow handles the entire catalog regardless of size.
AI Processing of Multiple Items
You have a list of customer reviews to analyze. Loop through each review, send it to an AI decision step for sentiment analysis, and store the classification alongside the review. The AI processes each review individually with full context.
Avoiding Infinite Loops
An infinite loop happens when the exit condition is never met. This is less of a risk with list-based loops (they end when the list is exhausted) but can happen with condition-based loops that repeat until a value changes. Always set a maximum iteration count as a safety limit. If your loop hits 1,000 iterations without completing, something is wrong, and the workflow should stop and alert you rather than running forever and consuming credits.
Process lists, batches, and collections automatically. Build loops into your workflows with no code.
Get Started Free