Skip to content

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.

  1. Evaluates the collection set in items.
  2. For each element, stores it in the itemVariable variable (and its 0-based position in indexVariable, if you set one) and runs the activities.
  3. 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 DataTableA row (DataRow): read its cells with str(row["Column"]).
A list or an arrayThe element as is (text, number, dictionary…).
A DictionaryA 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.

ParameterTypeDefaultDescription
itemsobject—The collection to loop through: list, array, DataTable (its rows) or Dictionary (its pairs). E.g. emails, table.
itemVariablestring—Name of the variable that holds the current element on each pass. E.g. item, row, email.
ParameterTypeDefaultDescription
indexVariablestring—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.

The activities that run on each iteration. They are configured as a lane on the canvas.

This activity does not return a value. What it produces are the itemVariable and indexVariable variables, updated on each pass.

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.

MessageCause 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 nothingThe collection is empty. Check that the previous activity returned data (for example, by logging rowCount(table) with a Log).
  • For Loop — repeat a counted number of times (when you are not looping through a collection).
  • While — repeat as long as a condition holds.
  • Break / Continue — control the loop from inside.