Workflow Logic

FOR_EACH

Iterate over any array in a Zafeguard workflow — run downstream stages once per item, with full parallelism and automatic aggregation.

FOR_EACH

FOR_EACH Workflow

FOR_EACH iterates over an array, spawning an independent subgraph execution for each item. All iterations run concurrently — the workflow only advances once all items have completed.


How it works

  1. Receives an items array from an upstream component
  2. For each item, spawns a copy of the downstream subgraph
  3. Passes the current item as a data output to downstream components
  4. Waits for all iterations to complete
  5. Aggregates results and passes control to the next stage
ARRAY_BUILDER (items: ["0xAddr1", "0xAddr2", "0xAddr3"])
        │ items[]

    FOR_EACH
        ├── item: "0xAddr1" ──▶ GET_EVM_ACCOUNT_BALANCE
        ├── item: "0xAddr2" ──▶ GET_EVM_ACCOUNT_BALANCE (parallel)
        └── item: "0xAddr3" ──▶ GET_EVM_ACCOUNT_BALANCE (parallel)

                                          ▼ (all complete)
                                    Next stage

Inputs

PortTypeDescription
itemsarrayThe array to iterate over. Each element becomes the item output for one iteration.

Outputs

PortTypeDescription
itemanyCurrent iteration value — type is inferred from the items array element type
indexnumberZero-based iteration index

Examples

Check balances for a list of addresses

ARRAY_BUILDER
  ├─ item 0: "0xAddress1"
  ├─ item 1: "0xAddress2"
  └─ item 2: "0xAddress3"
        │ array output

    FOR_EACH
        │ item (string — address)

GET_EVM_ACCOUNT_BALANCE
  ├─ jsonRpcUrl: <from STRING_CONSTANT>
  ├─ tokenAddress: "0x000...000"
  └─ account: <item>

Process a list of transactions

FOR_EACH (items: transaction_hashes[])
        │ item (tx hash)

WAIT_FOR_EVM_TRANSACTION
        │ receipt

API_CALL (report result to webhook)

Nested iteration (matrix)

FOR_EACH can be nested — the inner FOR_EACH runs for every item of the outer, creating a matrix of executions.

FOR_EACH (chains[])
  └── FOR_EACH (addresses[])
        └── GET_EVM_ACCOUNT_BALANCE

All iterations run in parallel. A FOR_EACH over 100 items spawns 100 concurrent stages. Ensure your plan's concurrency limits accommodate this. Credit usage scales linearly with the number of items × the cost of the inner subgraph.


Design tips

  • Aggregate results — use ARRAY_BUILDER downstream of FOR_EACH to collect all iteration outputs into a single array for further processing
  • Error handling — if any iteration fails, the entire FOR_EACH stage is marked failed. Use retry to re-run from the failed iteration subtree
  • Item type inference — connect the items port from a typed output (e.g. ARRAY_BUILDER) and the item output port will automatically inherit the element type in the visual builder

On this page