Skip to content

LOOP Step

The LOOP step allows you to iterate over an array in the pipeline and execute a set of steps for each element.

Properties

PropertyTypeDescription
typestringMust be loop.
instringPipeline reference to the array (e.g., ${items}).
itemstringVariable name to assign the current element to in each iteration.
indexstring(Optional) Variable name for the 0-based iteration index.
stepsarrayThe nested steps to execute for each item.

How it Works

  1. The engine resolves the array from the pipeline using the in path.
  2. For each element in that array:
    • It sets the element in the pipeline under the item name.
    • It sets the current index under the index name (if provided).
    • It executes all child steps.
  3. After the loop completes, the item and index variables are automatically removed from the pipeline to keep it clean.

Example: Processing Order Items

Assume the pipeline has an items array: [{"id": 1, "price": 10}, {"id": 2, "price": 20}].

yaml
- type: "loop"
  in: "${items}"
  item: "orderItem"
  index: "i"
  steps:
    - name: "LogItem"
      type: "invoke"
      service: "system.utils:Log"
      input:
        message: "Processing item ${i}: ${orderItem.id}"
    
    - name: "CalculateTax"
      type: "invoke"
      service: "system.math:Multiply"
      input:
        a: "${orderItem.price}"
        b: 0.1
      output:
        result: "itemTax"

Data Flow Scenario

Initial Pipeline:

json
{
  "items": [
    { "id": 1, "price": 10 },
    { "id": 2, "price": 20 }
  ]
}

During Iteration 1 (i=0):

json
{
  "items": [...],
  "orderItem": { "id": 1, "price": 10 },
  "i": 0,
  "itemTax": 1.0
}

During Iteration 2 (i=1):

json
{
  "items": [...],
  "orderItem": { "id": 2, "price": 20 },
  "i": 1,
  "itemTax": 2.0
}

Final Pipeline (After Loop):

json
{
  "items": [
    { "id": 1, "price": 10 },
    { "id": 2, "price": 20 }
  ],
  "itemTax": 2.0
}

(Note: 'orderItem' and 'i' were automatically removed)

Early Exit

You can use the EXIT step with from: "loop" to break out of a loop early when a certain condition is met.

yaml
- type: "loop"
  in: "${results}"
  item: "res"
  steps:
    - type: "exit"
      from: "loop"
      condition: "${res.status} == 'ERROR'"
      message: "Aborting loop due to error in data"

Tips

  • Scoping: Remember that item and index are updated in the same pipeline as the parent flow. If you have nested loops, make sure they use different item and index names to avoid collisions.
  • Large Arrays: Be mindful of performance when looping over thousands of items with many service calls inside.
  • Validation: Ensure the in property points to a valid array in the pipeline; otherwise, the step will fail.

Released under the ISC License.