For Each
For Each
Section titled “For Each”Type: foreach · Package: Core Activities v1.0.8
Loops through a collection —a list, an array, a dictionary, or the rows of a DataTable— and runs a block of activities once for each element. It is the loop you will use the most: “for each Excel row…”, “for each email received…”, “for each pending invoice…”.
On each pass, the current element is available in an iteration variable that you name and, if you ask for it, its position in another variable.
How it works
Section titled “How it works”- Evaluates the collection set in
items. - For each element, stores it in the
itemVariablevariable (and its 0-based position inindexVariable, if you set one) and runs theactivities. - Repeats until the collection is exhausted. With an empty collection, the body does not run and the flow continues.
What the iteration variable receives depending on items:
items is… | Each element is… |
|---|---|
A DataTable | A row (DataRow): read its cells with str(row["Column"]). |
| A list or an array | The element as is (text, number, dictionary…). |
A Dictionary | A pair with .Key and .Value. |
A text, a number, or an empty value (null) | It cannot be iterated: the activity fails (see Common errors). |
Inside the loop you can use Break (to exit early) and Continue (to skip to the next pass). The position in indexVariable keeps counting even when a pass ends with Continue, so element and position always match. Before each pass the loop checks whether the execution was terminated, so Terminate stops it between one element and the next.
Required parameters
Section titled “Required parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
items | object | — | The collection to loop through: list, array, DataTable (its rows) or Dictionary (its pairs). E.g. emails, table. |
itemVariable | string | — | Name of the variable that holds the current element on each pass. E.g. item, row, email. |
Optional parameters
Section titled “Optional parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
indexVariable | string | — | Name of the variable that receives the current element’s position, counting from 0 (0, 1, 2…). If you leave it empty, no index variable is created. |
Sub-flows
Section titled “Sub-flows”activities required
Section titled “activities required”The activities that run on each iteration. They are configured as a lane on the canvas.
Output
Section titled “Output”This activity does not return a value. What it produces are the itemVariable and indexVariable variables, updated on each pass.
Example
Section titled “Example”Loop through the rows of an Excel sheet, skip the ones without an email and say which sheet row each one is on:
For Each items = customerTable itemVariable = row indexVariable = n └─ activities: If condition = isEmpty(str(row["Email"])) └─ then: Log level = warning message = "Row " + (n + 2) + " has no email; skipped" Continue // skip this row and move on to the next one SMTP Send to = str(row["Email"]) subject = "Notice"Since n starts at 0 and row 1 of the sheet holds the headers, n + 2 is the row number a person sees in Excel.
Common errors
Section titled “Common errors”| Message | Cause and fix |
|---|---|
'items' is not iterable (…). | items is not a collection: it is a text, a number, or empty (null); the type received is shown in parentheses. Check that the previous activity returned a list or a table; if it can be empty, guard the loop with an If (items != null). |
'itemVariable' is required. | The iteration variable name is missing. Type it in the properties panel. |
'activities' is required. | The loop has no activities lane. Add at least one activity inside. |
| The loop does nothing | The collection is empty. Check that the previous activity returned data (for example, by logging rowCount(table) with a Log). |