# Repeat for each item

> Run the same steps once for every item of a list, such as the rows of a spreadsheet, then carry on with the count and the results.

Source: https://docs.go-lumen.ai/he/build/repeat-for-each-item

A for-each node runs the steps after it once for each item of a list, one item after the other, then goes on after the last one. Use it when a step produces a list, such as the rows of a spreadsheet, and you want to do something with every entry.

-   YAML or API
-   Any workspace

> **YAML for now:**
> 
> The flow editor does not have a for-each node to add yet. Write the loop in YAML and apply it with `lumen apply -f`. The console shows a run that went through a loop, with the node titled **For each item**, and you can open every step of every item.

## How a loop is shaped

A loop is three parts:

1.  The **for-each node**. It names the list and points to the first step of the body with `next`.
2.  The **body**: a straight line of nodes. Each runs once per item. The last one points to the end node.
3.  The **end node**, named by `end`. It runs once, after the last item.

```text
 read the rows ──> for each row ──> send a reminder ──> tell the team
                    (items)           (body, once per row)  (end, once)
```

| Setting | Meaning |
| --- | --- |
| `items` | One placeholder that names a list, such as `{sheet.rows}`. Required. |
| `maxItems` | The most items the list may have. Default 100, at most 1000. |
| `end` | The node that closes the loop and runs once after the last item. Required. |

A for-each node has exactly one `next`, the first node of the body.

## An example

A supplier list arrives as a spreadsheet attached to an email. The flow reads its rows, sends each supplier a reminder, then tells the team how many it sent.

```yaml title="reminders.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: foreach-supplier-reminders
  namespace: tenant-acme
spec:
  displayName: Remind every supplier on the list
  triggers:
    - id: email
      ref: {name: email-arrives}
      connectorRef: {name: invoices-inbox}
      config: {attachments: together, requireAttachment: true}
      next: [sheet]
  nodes:
    - id: sheet
      title: Read the supplier list
      stepRef: {name: read-spreadsheet}
      next: [suppliers]
    - id: suppliers
      title: For each supplier
      forEach:
        items: "{sheet.rows}"
        maxItems: 200
        end: done
      next: [remind]
    - id: remind
      title: Send the reminder
      stepRef: {name: send-email}
      config:
        to: "{each.item.email}"
        subject: "Please confirm your details, {each.item.name}"
        body: "Hello {each.item.name}, this is reminder {each.index}."
        attach: false
      next: [done]
    - id: done
      title: Tell the team
      stepRef: {name: notify-in-app}
      config:
        to: members
        title: Reminders sent
        body: "{suppliers.count} suppliers were reminded."
```

Each cell of a row is text, keyed by the column name in the sheet's first row, so a sheet with the columns `name` and `email` gives `{each.item.name}` and `{each.item.email}`.

## What the body sees

Inside the body, every node can read everything that was known before the loop, plus two fields for the current item:

| Placeholder | Holds |
| --- | --- |
| `{each.item}` | The current item. |
| `{each.item.name}` | A field of the item, when the item is an object (a row). Nested fields and list positions work too: `{each.item.address.city}`, `{each.item.tags.1}`. |
| `{each.index}` | The item's position in the list, counting from 0. |

An item is processed completely, body and all, before the next one starts. They run one after the other, in list order, never in parallel.

The fields that body steps make belong to that one item. They are not visible after the loop: the end node sees the document as it was when the loop began, plus the two outputs below.

## What comes back

The end node runs once, after the last item, with:

| Field | Holds |
| --- | --- |
| `<id>.count` | How many items the loop went through. In the example, `{suppliers.count}`. |
| `<id>.results` | A list with one entry per item, in order. Each entry holds the fields that the body's last node added for that item, as an object. |

An item that a filter in the body cut short adds no entry. An empty list skips the body: the loop goes straight to the end node with a count of 0 and an empty list of results.

Use `results` as the `items` of a second loop, or read an entry in a condition or a placeholder, such as `{suppliers.results.0}`.

## Rules and limits

-   The list must be a list. If the placeholder names something that is missing or is not a list, the run fails at the for-each with a message that says so.
-   A list longer than `maxItems` fails the run: "the list has 250 items, more than its limit of 200". Raise `maxItems` (at most 1000) or narrow the list first, for example with a filter in the step that produces it.
-   The body is a straight line. Each body node leads to exactly one next node, ending at the end node. A branch, a join, a node reached from outside the body, or another for-each inside it is refused when you save the flow. Loops do not nest.
-   If a step in the body fails, the run fails there, as any step does. See [When a run fails](https://docs.go-lumen.ai/he/build/when-a-run-fails.md).
-   The for-each node costs half a credit. The steps in the body cost what they cost, once per item, so a loop of 100 emails costs 100 times the email step.
-   A disabled for-each skips its body and goes straight to its end node. See [Steps](https://docs.go-lumen.ai/he/build/steps.md#disable).

Besides a field that holds a list, `items` can be `{files}`, which loops over the files of the document: each item describes one file.

## Next steps

[Branches and filtersDrop an item with a filter inside the loop, or send each result down a path afterwards.](https://docs.go-lumen.ai/he/build/branches-and-filters.md)[When a run failsWhat happens when a step in the loop cannot finish.](https://docs.go-lumen.ai/he/build/when-a-run-fails.md)[SpreadsheetsRead the rows a loop goes through, and write results back.](https://docs.go-lumen.ai/he/data/spreadsheets.md)[Runs and retriesSee each item's steps in the run view.](https://docs.go-lumen.ai/he/operate/runs-and-retries.md)
