# For each (/docs/nodes/for-each)



The **For each** node loops over a list. For every element it runs a published decision and collects the answers, in order, into a new list.

## When to use it [#when-to-use-it]

* Evaluate every transaction, line item or applicant of a batch with the same rules.
* Keep the per-item logic in one decision instead of copying nodes.

## How it works [#how-it-works]

<Diagram node="for-each" kind="anatomy" />

1. The **items** expression produces the list. `null` counts as an empty list; anything that is not a list is an error. A list longer than the limit is refused (1,000 by default).
2. For each element, in order, the **input mapping** builds the child payload. The mapping can read `item` (the element) and `index` (its position), besides the usual scope.
3. The child decision runs, and its outputs are added to the result list.
4. When all elements are done, the list is stored and the flow continues.

## Ports [#ports]

| Port   | Taken when                      |
| ------ | ------------------------------- |
| `Next` | Always, after the last element. |

## Configuration [#configuration]

| Field            | What it does                                                                 |
| ---------------- | ---------------------------------------------------------------------------- |
| Name             | Label shown on the canvas and in the trace.                                  |
| Items            | The expression that yields the list.                                         |
| Decision         | The published decision to run per element. Its version is pinned at publish. |
| Input mapping    | Child input field to expression; it can use `item` and `index`.              |
| Store results in | The variable that receives the list of outputs. Required.                    |
| Max items        | Refuses longer lists instead of running forever.                             |

## What it writes [#what-it-writes]

A list under your variable, one entry per element and in the same order: `vars.results[0].flagged`. The trace contains the child trace of every item.

## In a real flow [#in-a-real-flow]

<Diagram node="for-each" kind="example" />

Every transaction of the payload is scored by a child decision. A condition then checks whether any result was flagged.

## When it fails [#when-it-fails]

* One element fails: the whole node fails. A list evaluated half-way is not an answer.
* The value is not a list (`VALIDATION_FAILED`) or has more elements than allowed.
* No decision, no list expression or no output variable are errors in Checks.

## Good to know [#good-to-know]

* Elements run one after another so the trace reads in order. For independent, different checks at once, use [Parallel](/docs/nodes/parallel).

## Related [#related]

* [Sub-decision](/docs/nodes/sub-decision)
* [Parallel](/docs/nodes/parallel)
