The scatter-gather pattern is a powerful way to process lists of data.

If you encounter lists of data (e.g. alerts, messages) in your workflows that you want to:

  • Filter with a simple if-condition (see if-conditions)
  • Process one at a time: rename fields, create a case, etc.

We recommend first scattering the list of data into individual items, processing them one at a time, then gathering them back into a single list.

It’s almost always easier to deal with data one at a time than as a list!

After scattering, chain actions like you would in a workflow that processes one data point at a time. The output for every action between a scatter and gather action will be shown as N individual items in the workflow runs view.

Scatter

The scatter action takes a list of data and scatters it into individual items. Every action after a scatter action will be run for each item in the list.

collection: [
  {
    "name": "John",
    "age": 30,
    "city": "New York"
  },
  {
    "name": "Jane",
    "age": 25,
    "city": "Los Angeles"
  }
]

Gather

The gather action ONLY works downstream of a scatter action. It will not work if you try to gather data that was not scattered.

The gather action takes a list of scattered data and gathers it back into a single list.

items: ${{ ACTIONS.some_action_after_scatter.result }}

Scatter, filter, then gather

In this example, we’ll scatter a list of integers, filter out the odd numbers with a simple if-condition on a reshape action, then gather the even numbers back into a single list.

1

Scatter

Scatter the list of integers into individual items.

2

Filter

Filter out the odd numbers with a simple if-condition on a reshape action. Set the inputs for the reshape action to:

value: ${{ ACTIONS.scatter.result }}

And the if-condition to:

${{ FN.mod(ACTIONS.reshape.result.number, 2) == 0 }}

3

Gather

Gather the even numbers back into a single list.

Set the inputs for the gather action to:

items: ${{ ACTIONS.reshape.result }}

4

Run workflow

Notice how the results for actions after the scatter action are shown as N individual items.