Loop node

Use the loop node to execute one or more nodes in a loop. Nested loops are not supported. There are two types of loops:

Do while
This loop evaluates the condition before running. If the condition is true, the loop runs, otherwise it continues with the next node. This loop type is used when the condition is dependent on the workflow processes that occur before the loop node. The process defined by the loop does not run if the condition is already met.

Do until
This loop will evaluate the condition after each execution of the loop nodes. The workflow completes the process defined in the loop before checking the loop condition. If the condition is true, the loop runs again, otherwise it continues with the next node after the loop. This loop type is used if the process defined by the loop must run at least one time regardless of any previous activities.

We can also set the following option.

Asynchronous Processing of the Loop Body
When set, the loop runs unblocked. Each iteration of the loop runs without waiting for the previous iteration to finish. If not set, then each iteration of the loop must finish before the next iteration of the loop can start.

Nodes contained within the loop must not change to any activities outside the loop. All transitions must originate to and from the loop node. The standard rules for split and join apply for loops in terms of multiple transitions in and multiple transitions out. The loop node does not specify the results of the nodes in the loop. We must check the status of nodes in the loop in a script that follows the loop, if required.

An activity in the loop can run multiple times (one time for each loop iteration). The workflow engine tracks the activities in a loop by giving them an index that represents to which iteration of the loop the instance of the activity applies. This index is stored in the activity object as a member variable called index. For activities that are not in a loop, this index is set to 0. For activities in a loop, this value is 1-n, where n represents the number of actual loop iterations that run.

To retrieve an instance of an activity in a loop, use the process.getActivity() method. This method takes two arguments: the activity ID and the index of the target activity instance. If we use this method to retrieve an activity that is not part of a loop, we can either omit the index argument or set it to 0. The method returns an activity object.

The condition defined in the loop specifies the number of loop iterations. The global variable, loopcount, can be used to identify the current iteration of the loop. The loopcount variable starts with 1 (loopcount=1) and increments each time that the loop is run. This variable can be useful for creating loop conditions, for example, loopcount<x.

Nodes placed in the loop determine the amount of time each loop takes. The loop node does not have a built-in wait mechanism.

The activity ID and the loop condition are the only required properties for the loop node.

Parent topic: Workflow elements