for-each runs one heading body once per element of a list the run already has, such as the
policies in a batch, and gathers per-item results into lists ordered like the input. Each item
can call your systems, route, or ask a person or an agent session. Use
repeat when the number of rounds depends on what each round
produces, and parallel for a fixed set of different
workstreams.
Syntax
for-each id=send-notices
Fields
The fence takes no attributes after
id, and its body takes no other fields.
The body
Thefor-each follows the same heading rules as a
choice: it is the last fence in its
section, body is a direct child heading of that section, and the run continues at the next
heading after it.
- The body can hold any steps: calls, values, tasks, agents, waits, and nested
choice,repeatorfor-eachconstructs. Tasks and agents are allowed at anyconcurrency. - The item name and every value the body binds belong to that item. Items running at the same time can’t see or overwrite each other’s values. The item name can’t reuse a name that is already visible.
- Every value a
collectentry reads must be bound on every path through the body. completeisn’t allowed in the body. Ending the run from one item would cut the other items short. Route or complete after thefor-each, using the collected lists.
Collected lists
Eachcollect entry becomes a list named after the entry, such as $receipts, that later steps
can read. The list is in item order, the order of items, whatever order the items finish in. It
is checked against its schema when the for-each ends. Values the body binds that aren’t
collected can’t be read after the for-each.
An empty items list runs no item and produces empty lists.
Limits and failures
- A list with more than 1,000 items fails the run with
failure.codefor_each_limitwhen thefor-eachstarts. - If one item fails, for example its effect runs out of retries, the run fails. No new item starts, open tasks of items still running are cancelled before the run ends, and no collected list is produced. A write that an earlier or concurrent item already sent may still reach your system; failing the run doesn’t undo it.
- To handle an expected per-item problem, such as a policy the mail vendor refuses, return it as
data from the item’s effect, for example a
statusmember, and route on it after thefor-each. Then every item still collects a result.
Example
This procedure sends a past-due notice for every policy in a batch, five at a time. The effect sends the step’s idempotency key so the mail vendor can drop a duplicate if a retry repeats a request. The vendor host and policy values are illustrative.past-due-notices.procedure.md
NW-100234, NW-100517 and NW-100902, the run ends with
policyNumbers: ["NW-100234", "NW-100517", "NW-100902"] and one receipt for each, in that order,
even if the second notice is queued last.
Ask a person about each item
An item body can hold a task. Withconcurrency: 2, at most two review tasks are open at a time,
and the verdicts are collected in item order. This excerpt leaves out the schemas.
On retry and resume
- Each item is its own step. The same step has a different identity for each item. Each
item’s call, task or agent session runs once for that item, and an effect’s
operation.idempotencyKeydiffers between items and stays the same across retries of one item. - Finished items aren’t run again. When a run resumes, items that finished keep their recorded results. Only unfinished work continues.
- Order doesn’t depend on timing. With
concurrencyabove 1, items finish in any order, but collected lists are always in item order. New items start in list order as earlier ones finish. - All lists appear together. Collected lists become visible in one step when the last item finishes, never partly.
- Failure cleans up. When one item fails, open tasks of other items are cancelled before the run is marked failed.
Common errors
At run time, a list longer than 1,000 items fails the run with
for_each_limit. Split large
batches before the for-each, or run them as separate runs.