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
| Property | Type | Description |
|---|---|---|
type | string | Must be loop. |
in | string | Pipeline reference to the array (e.g., ${items}). |
item | string | Variable name to assign the current element to in each iteration. |
index | string | (Optional) Variable name for the 0-based iteration index. |
steps | array | The nested steps to execute for each item. |
How it Works
- The engine resolves the array from the pipeline using the
inpath. - For each element in that array:
- It sets the element in the pipeline under the
itemname. - It sets the current index under the
indexname (if provided). - It executes all child
steps.
- It sets the element in the pipeline under the
- After the loop completes, the
itemandindexvariables 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
itemandindexare updated in the same pipeline as the parent flow. If you have nested loops, make sure they use differentitemandindexnames to avoid collisions. - Large Arrays: Be mindful of performance when looping over thousands of items with many service calls inside.
- Validation: Ensure the
inproperty points to a valid array in the pipeline; otherwise, the step will fail.
