Files
directus/docs/cookbook/flows/flows-for-loop.md
Rijk van Zanten c723085902 Move docs to monorepo (#18052)
* Move docs to monorepo

* Tweaks
2023-04-04 18:23:58 -04:00

3.0 KiB

description, tags, skill_level, directus_version, author_override, author
description tags skill_level directus_version author_override author
When most flows begin, they pass the trigger's payload to the data chain and execute once. This recipe explains how to execute a flow for each element in a payload's array.
9.18.1 Eron Powell

Flows For-Loop

{{ $frontmatter.description }}

:::tip Author: {{$frontmatter.author}}

Directus Version: {{$frontmatter.directus_version}}

:::

Explanation

Sometimes you may have an array of data you'd like to iterate over and run operations on, one-by-one. However, you may have noticed that each operation in a flow executes just one time. Because of this, you can't create a "for-loop" within the operations of a single flow.

However, to achieve a "for-loop", you can instead use the trigger flow operation to pass the data into an another flow trigger. When this type of trigger receives an array as a Payload, the flow runs for each item in the array individually.

:::tip

Remember: for some use-cases, you can also iterate through data in a Run Script operation.

:::

The Recipe

:::tip Requirements

You'll need a flow with an array of data on its data chain.

:::

Configure the Starting Flow

  1. Configure a flow a trigger flow operation.
  2. Under Payload, be sure to add the desired array.
  3. Save and exit the flow.

Configure the "For-Loop" Flow

Once your starting flow is configured as desired, follow these steps.

  1. Create a flow using the another flow trigger.
  2. Configure operations as desired.

Final Tips

Once your for-loop is configured, you can process the data several ways.

First, you could simple let the "for-loop" flow process each element in the Payload.

Second, you could also configure a Response Body in the trigger of your "for-loop" flow. The Response Body gets appended under the trigger flow operation in the starting flow.

Third, you could add another trigger flow operation into the "for-loop" flow, to create complex flow chains. If you do this, just keep API performance in mind. If you configure a Response Body, the parent flow will halt execution until it receives Response Body.

Good luck and have fun! 🧑‍🍳