Repeat for each

Repeat for each runs steps once for every item of a list, one item after the other: every line item of an order, every order a query found, every file in a folder. When the list is done, the run continues under After the loop.

Add a loop

  1. Press + and pick Repeat for each.
  2. In List, pick ONE variable that holds a list, with the variable button on the right. For example:
{{ event.payload.line_items }}
  1. Press + under For each and add the first step to repeat. Add more steps below it.
  2. Choose When an item fails: Stop the loop and fail the run, or Go on with the next item.
  3. Optional: set Maximum items. Empty means 100; the most is 500.

What the repeated steps read

Variable Holds
loop.item The current item. Its fields as loop.item.sku, loop.item.id and so on
loop.index The position, starting at 0
loop.number The position, starting at 1
loop.length How many items the list has
loop.first, loop.last true for the first and the last item
loop.parent.item In a loop inside a loop: the current item of the outer loop

Repeated steps also read each other's results for the same item, as usual: steps.<id>.output. After a test run the variable picker offers the real fields of an item.

What steps after the loop read

The results of the repeated steps are cleared between items. After the loop, read the loop's own result, steps.<loop>.output:

Field Holds
count How many items the list had
succeeded, failed How many items went through, and how many failed
results Per item: index, status, and the output of its last step (or the error)

A result over 4,000 characters is left out of results; it stays in the run history. A typical last step is a Log:

Archived {{ steps.each_order.output.succeeded }} of {{ steps.each_order.output.count }} orders

When an item fails

  • Stop the loop and fail the run - the run ends as Failed at that item. Items before it stay done.
  • Go on with the next item - the item counts as failed, the loop continues, and the run goes on after the loop. A Fail the run step inside the loop then fails only the current item. Check steps.<loop>.output.failed afterwards if failures matter.

Loops inside loops

A loop can contain another loop, for example every order, and in it every line item. Inside the inner loop, loop is the inner item and loop.parent the outer one. Keep the 1,000 step limit in mind: the numbers multiply.

Where lists come from

  • The event: event.payload.line_items, event.payload.discount_codes, event.payload.added_tags.
  • A Shopify query: steps.orders.output.orders.nodes. See Every Shopify mutation and query.
  • A File transfer step that lists a folder: read each file's path as loop.item.path. See Files, SFTP and downloads.
  • A Liquid script step that builds a list, for example from the lines of a CSV file. See Liquid script step.
  • An HTTP request: a list in steps.request.output.body.

In the run history

A repeated step appears once per item, grouped by item, with what it returned. The loop's own entry comes last, because it is written when the loop ends.