While Loop
Use the While Loop step to repeat a sequence of actions for as long as a condition stays true. Unlike the For Each Loop, which runs once per item in a known collection, the While Loop keeps going until its condition turns false — so it fits open-ended work where you do not know the number of passes up front.
Typical use cases include:
- Polling until ready. Call an external system on each pass and keep looping while its status is still
pending. - Draining a queue. Keep processing while a collection still has items, removing one per pass inside the loop body.
- Counting up to a threshold. Increment an element each pass and loop while it is below a target value.
Configure the step
Open the flow editor, add While Loop, and fill in the configuration card.
Description
- Purpose: Make the loop's intent clear at a glance.
- When to fill it: Always. The description is shown in the editor and execution history.
- Tips: Name the condition you are looping on, for example,
While status is pendingorWhile queue not empty.
Mode
- Purpose: Choose when the condition is checked.
- When to fill it: Always. Two modes are available:
- While (check condition first) — the condition is evaluated before each pass. If it is already false, the loop body runs zero times. This is the classic pre-test loop.
- Do-While (run body first) — the loop body runs at least once, and the condition is checked after the first pass. Use this when the work must happen before you can decide whether to continue.
Max. iterations
- Purpose: A safety limit on how many passes the loop may make.
- When to fill it: Always. Defaults to
1000. Must be at least 1. - Tips: A While Loop is only as safe as its condition. If the condition never becomes false, this limit stops the run with an error instead of looping forever. Size it to the largest pass count you legitimately expect, with some headroom.
Condition
- Purpose: A SmartField filter expression that is re-evaluated on every pass. The loop continues while it is true.
- When to fill it: Required. Uses the same expression syntax as the Decision step — comparators such as
=,>,contains, combined with the connectors&,|and!(...). - Tips: Use the SmartField picker (assist-edit) to insert placeholders. Because the condition is re-resolved each pass, reference values that the loop body changes (an element you increment, a collection you drain) so the loop can actually terminate.
Behavior
While Loop produces one output that downstream and in-loop steps can read:
iteration— the 1-based index of the current pass.
When the step runs:
- On each dispatch, AutoFlow re-resolves the configuration against the current context — so the condition reflects any changes the loop body made on the previous pass.
- In While mode it evaluates the condition immediately. In Do-While mode it forces the first pass and only starts evaluating the condition from the second pass on.
- If the condition is true, the steps connected to the while edge run, and the step asks the engine to come back for another pass.
- If the condition is false, the body is skipped and the loop ends — the flow continues with the steps after the While Loop.
If the condition is still true once iteration exceeds Max. iterations, the step fails with The While loop did not terminate within its configured maximum of <n> iterations. and the execution stops. A loop that ends naturally on the boundary pass does not error.
Best practices
- Always change something the condition reads. A While Loop only terminates when its condition can turn false. If the body never touches the values in the condition, the loop runs to the Max. iterations limit and errors. Increment a Create Element value, drain a collection, or re-fetch a remote status inside the body.
- Prefer For Each for known collections. If you already have the full set of items, the For Each Loop is clearer and self-bounding. Reach for While only when the pass count is open-ended.
- Set Max. iterations deliberately. Treat it as a circuit breaker, not a loop counter. Make it comfortably larger than the worst legitimate case so a healthy run never trips it, but small enough that a runaway fails fast.
- Use Do-While when the first pass is unconditional. If you must run the work once before you can judge whether to continue (for example, fetch-then-check), Do-While avoids duplicating that first action before the loop.