FlowmageDocs

Repeat nodes for each item in a list

Use a Loop to run the same nodes once for every item in a list, read the current item, end early with Break, and see each round in the run.

The Fetch every page of an API into a table template uses a Loop this way:

  1. Start
  2. Fetch every page
  3. Each item
  4. Store the item
Fetch every page of an API into a tableDownloadOpen in Flowmage

Steps

Add a Loop and point it at the list

Click the + after the node with the list, pick Loop under Decide & repeat, and open it. Click Items, then the list under Input, such as lines under Start. Below the field, Flowmage counts the items, such as 2 items.

The Loop Each order line with Items set to Lines from Start, and below it 2 items

Add nodes to both ports

Close the dialog and click the + on each of the Loop's ports:

  • Each item: the nodes that run once per item. They read the current item as loop.item: a Log with the Message Line {{ loop.item.sku }} writes Line TEA, then Line MUG.
  • After the last: what runs once the list is done. Its input has Item count and Completed count, as in a Log with {{ json.completedCount }} lines done.
The Loop Each order line on the canvas, with Note the line on Each item and Send the confirmation on After the last

Test the rounds

Click Test run. Under Steps, the Loop shows its rounds, such as 2 / 2: click the arrow beside it to list them, and a node in a round to see what it did there.

A run with the Loop opened at 2 / 2 under Steps: Note the line once for item 1 and once for item 2, above Note the count

End a loop early

Add a Break after a node on Each item. When a run reaches it, the Loop stops, the flow goes on from After the last, and the Loop says where it ended, such as Stopped by Break at item 1 of 2.

A run of a Loop with a Break after its first node: the Loop stopped at item 1, and Note the count ran with stoppedEarly true
Work on a whole list without a loop

Lists, under Advanced in Add a step, works on a whole list in one node: List — Filter, List — Sort, List — Group, List — Limit and List — Remove duplicates. Use one to shape a list, and a Loop when each item needs its own nodes.

If it did not work

  • "Items is required." Pick the list from Input, or write an expression that gives one.
  • A value inside the loop is empty. The first node inside a loop gets no json. Read the item as loop.item.
  • The flow does not go on after a round. A round ends at the last node on Each item. Never link it back to the Loop: what runs once goes on After the last.

Next