# Loop

The **Loop** tool allows you to iterate over a list of items one-by-one.

A Loop is useful when you want to take a list of things, for example, all of the line items in an order, and perform an action on each individual item.

## Configure <a href="#overview" id="overview"></a>

### Loop

To iterate over specific items from a list (typically from the [Trigger](https://docs.getmesa.com/workflow-builder/triggers) or [Actions](https://docs.getmesa.com/apps/shopify/actions)), use the [variables](https://docs.getmesa.com/workflow-builder/fields/variables) menu to select the list of items after adding your Loop step.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2F5PlhFvTQEvhDTqDx4bzp%2Fimage.png?alt=media&#x26;token=b3c85040-ab31-4887-bf12-d6b3d2b72ef5" alt=""><figcaption></figcaption></figure>

After your Loop has been added and configured, you'll see your Loop steps within a confined container with a grey border.

To perform one or more tasks on each item in the list, add [actions](https://docs.getmesa.com/apps/shopify/actions) after the Loop within the container with a grey border.

You can reference the individual items or their information by using the Loop variables that will populate for actions added in the Loop container.

You will have variables available from steps that occur prior to the Loop in your workflow, as well as the Loop variables. Typically, you'll want to use Loop variables when configuring actions within the Loop container.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FFMVEPVBTZOflPBv6G9VQ%2Fimage.png?alt=media&#x26;token=975bb36b-1bec-42bc-b038-513878c24fc7" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Be sure to **Save** your workflow after adding and configuring your Loop to see your available Loop variables.
{% endhint %}

In each Loop action, there are conditions you can add to filter the list.&#x20;

You can populate the [Filter by conditions](https://docs.getmesa.com/tools/filter#examples) via the More fields button so that specific values or results that meet the conditions are passed to the following steps.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2F7C1s8ZcHxH93zhBTiWRH%2Fimage.png?alt=media&#x26;token=1ea42ed2-4dfc-4144-b69c-7f1a48914979" alt=""><figcaption></figcaption></figure>

## Going Further

### Loop End&#x20;

The default configuration of the Loop tool will continue the workflow without passing variables after the Loop. However, you can optionally configure a Loop End step to build a list from matching items to use in later steps.

In the Loop step, click the More options button to add the Loop End via the Build a list from matching items checkbox.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FZS6ZJqlAiuzphhaDGbLs%2Fimage.png?alt=media&#x26;token=678ecd08-b00e-49dd-80f1-598dfaa963fb" alt=""><figcaption></figcaption></figure>

After saving your workflow, use the [variable](https://docs.getmesa.com/workflow-builder/fields/variables) selector to append data from the Loop container to use in future steps after the Loop.

The value can be a single variable from an action within the Loop, or you can add a [Transform Mapping](https://docs.getmesa.com/tools/transform#transform-mapping) action within the Loop and map a new object.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FZYbJr11U8VRVsPNdjw0B%2Fimage.png?alt=media&#x26;token=b0844f6b-2071-45e6-99d2-175076064933" alt=""><figcaption></figcaption></figure>

The variable that's created from the Loop End step will be Loop End > Items, and it can be used in steps after the Loop.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FlbZXiRSeLDmVTwk1B15Y%2Fimage.png?alt=media&#x26;token=408ea747-f49f-4e6a-b2f8-852cd40a35e1" alt=""><figcaption></figcaption></figure>

### Other kinds of Loop steps

Beyond the regular Loop action, you can add different variations of Loop actions to your workflow. These are single-output steps with a different behavior than the Loop & Loop End functionality.

#### Sum

The Loop Sum action will calculate the sum of all items that match a specific criteria.

For example, the Loop Sum action can add up the price of line items (products) in an order and pass that value as a variable to reference in the following steps. The value will be an integer.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FbVrY1tpr53raodO9KAge%2Fimage.png?alt=media&#x26;token=4c643d5c-d727-498c-9a38-f07c86f88cf3" alt=""><figcaption></figcaption></figure>

#### Map

The Loop Map action will create a list of values that are identified based on the item you're referencing from a previous step.

For example, the Loop Map action can pass all line item (product) titles in an order for tagging purposes.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FK3SoQXyM0b5Ke0vCOlwJ%2Fimage.png?alt=media&#x26;token=a1c9e7e7-779c-4179-8544-fc62eb7de405" alt=""><figcaption></figcaption></figure>

#### Number of Matches

The Loop Number of Matches action will return the number of items that match a specific criteria.

For example, the Loop Number of Matches action can provide an integer as a variable that can be referenced in future steps representing the number of line items (products) in an order with a specific SKU.

<figure><img src="https://3425906282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1H6u1HQc3Iew7ATmmiCi%2Fuploads%2FTR1elJMmCiNny5DGeN5s%2Fimage.png?alt=media&#x26;token=9bab6ef9-09c6-4ce2-b591-b0fa18dbe74d" alt=""><figcaption></figcaption></figure>

## Technical Notes

* The Loop queue is synchronous.
* There is a legacy version of the Loop step that can be found on stores with previous versions of MESA, or when using templates that have not been updated to the latest Loop and Loop End functionality.
  * Everything gets enqueued all at once with legacy Loop actions, which makes this action more susceptible to timeouts.


---

# 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.getmesa.com/tools/loop.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.
