# Foreach

The Foreach action node loops over objects in a [data source](/reference/apps/data/data-sources.md) and runs a block of action nodes for each object.

## Usage

This action node is used to run a block of other action nodes repeatedly. The block will be run once for each object in the data source according to the object selection. On each iteration, the current object becomes the [object in context](/reference/platform-concepts/objects.md#object-in-context).

{% hint style="info" %}
**Good to know**

If you want to update or delete a selection of objects use the [Update object](/library/action-nodes/update-object.md) or [Delete objects](/library/action-nodes/delete-objects.md) action nodes.
{% endhint %}

### Example

{% tabs %}
{% tab title="Loop through active projects" %}
Your app has a data source containing all projects. You'd like to iterate over active projects to calculate resource utilization.

Use Foreach to loop over a filtered selection of projects with a filter `Project.State EQUALS Active` to only iterate over active projects.
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Showroom**

A Foreach example is available in our [Showroom](https://showroom.appfarm.app/action-nodes/foreach). You can view the demo, as well as access the setup in Appfarm Create. Don't have access? [Register](https://showroom.appfarm.app/sign-up).
{% endhint %}

### Working with schemas

ForEach can iterate over an [action variable](/reference/actions/action-variables.md) or [action param](/reference/actions/action-params.md) of type [schema](/reference/platform-concepts/schemas.md), provided the schema is an array. On each iteration, the current element becomes available as the object in context.

A single ForEach node descends one array level only. To iterate a nested array — for example, to loop over each `Address` on each `Company` — use two ForEach nodes: the outer node iterates the `Company` array, and an inner node iterates the `Address` array on each company.

{% hint style="info" %}
**Good to know**\
Array-of-arrays schemas are not supported. If you need to represent a nested list, wrap the inner array in an object. For example, instead of an array of arrays of strings, define an array of objects where each object contains an array of strings.
{% endhint %}

## Properties

<table><thead><tr><th width="213">Property</th><th>Description</th></tr></thead><tbody><tr><td><strong>Data Source</strong></td><td>Select a multi-cardinality data source containing the objects to iterate over.<br><br>You may also select a multi-reference property (an <a href="/spaces/-MiLU-xcHu0eLZiTxcmZ/pages/hTZnyJiBPuAf0NHTWpiq#general-properties">Object Class Property with Cardinality <code>Many</code></a>), and iterate over the referenced objects.</td></tr><tr><td><strong>Selection</strong></td><td><p>Define the objects to update.</p><ul><li><code>All objects</code></li><li><a href="/spaces/-MiLU-xcHu0eLZiTxcmZ/pages/-MiR92uc7YpwXZWPxMI1#object-selection"><code>Selected object(s)</code></a></li><li><code>Filtered selection</code></li></ul></td></tr><tr><td><strong>Filter</strong></td><td><em>Available when</em> <strong>Selection</strong> <em>is Filtered selection.</em><br><br>Define the <a href="/spaces/-MiLU-xcHu0eLZiTxcmZ/pages/-MiR9GGOxcZeghNQPbzV">filter</a> to apply to the data source.</td></tr><tr><td><strong>Execution Mode</strong></td><td><strong>Sequential</strong>: The default. All action nodes inside the Foreach loop will be executed in sequence.<br><br><strong>Parallel</strong>: Execute all action nodes inside the Foreach loop in parallel.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.appfarm.io/library/action-nodes/foreach.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
