Skip to main content

Node - Loop


Function Overview

The Loop node allows a portion of the workflow to be executed repeatedly. Looping can be configured either for a specific number of times or based on certain conditions, continuing until an exit condition is met.

  • Loop based on conditions

    The workflow will continue to loop through a specific process until the parameters meet the defined exit condition.

  • Loop a specific number of times

    Loops are executed strictly in sequence — each iteration is carried out one after the other.

Loops are executed strictly in sequence — each iteration is carried out one after the other.

Each loop iteration increases the workflow execution count by one.

Loop a specific number of times

Example: Randomly select 10 questions from a question bank of 100 questions to generate a test paper.

Implementation: Use the “Get Single Data” node to randomly retrieve one question, duplicate the question and link it to the test paper, and mark the original question as “Retrieved.” Repeat this process 10 times. After the loop ends, remove the retrieval marks from the question bank.

Add a Loop node

In the workflow, add a “Loop” node and select “Loop a specific number of times”.

Configuring the Loop node

Parameter settings

When using the “Loop a specific number of times” mode, there are three system-defined parameters: start, end, and step. Only the start values can be configured—these parameter names cannot be deleted or modified.

  • start: The starting value of the loop.

  • step: The increment value added to start after each loop iteration. For example, if start = 1 and step = 2, after the first iteration, start becomes 3.

  • end: The ending value of the loop. The loop exits when start exceeds end, and no further iterations are executed.

In addition to these fixed parameters, you can also pass in custom parameters. For example, in this case, each loop iteration needs to access the newly created test paper record. Therefore, the test paper record ID (exampaperid) should be passed into the loop. Within the loop, the record can be retrieved using this ID.

Add new parameters

To add new loop parameters, they must be defined in the first node within the loop process.

Loop exit condition

When using the “Loop a specific number of times” mode, the loop can only exit based on a comparison between the start and end values.

Execution count examples:

  • start = 1, step = 1, end = 3 → The loop executes 3 times. After the third iteration, start value becomes 4, which is greater than end, so the loop ends.

  • start = 1, step = 2, end = 3 → The loop executes 2 times. After the second iteration, start value becomes 5, which is greater than end, so the loop ends.

  • start = 1, step = 1, end = 0 → The loop executes 0 times, since the initial start value already exceeds end.

Maximum loop count

Loops cannot run indefinitely—the Loop node supports a maximum of 10,000 iterations. Once this limit is reached, the loop will automatically terminate and proceed to execute the subsequent nodes.

Even if the start value is less than the end value, the loop will exit if the maximum number of iterations is reached.

Pass the triggering user

If the option “Set the trigger of the loop process to be the trigger of this process” is selected, the trigger of the loop process will inherit the trigger user of the main workflow. If this option is not selected, the trigger of the subprocess will default to “Workflow.”

Process interrupted in loop

If an iteration within the loop is interrupted or terminated, there are three options for how the workflow should proceed:

  • Skip and continue to next iteration: Ignore the error or termination and start the next loop iteration.

  • Exit the loop and continue with the workflow: End the entire loop immediately and proceed to execute the nodes following the loop node.

  • Terminate the workflow: Immediately terminate the entire workflow where the loop node resides.

Configure the loop process

Click the button within the loop node to enter the loop process and configure the action nodes inside the loop.

Loop Process Configuration:

The process below will be executed 10 times. After each iteration, a random question is retrieved, and the start value is incremented by 1 to proceed to the next iteration. After the 10th iteration, the start value becomes 11, which exceeds the end value, and the loop stops.

If the question bank contains only 6 questions and cannot fulfill the required 10, then during the 7th iteration, when no question can be retrieved, the “Terminate Process” node will be triggered to stop the workflow and exit the loop.

Complete the workflow

After the loop ends, all questions marked as “Retrieved” will have their marks cleared. The entire workflow configuration is now complete.

Loop when conditions are met

In the “Loop when conditions are met” mode, there is a fixed system parameter called index, which automatically increments by 1 after each iteration. The condition to exit the loop is determined using custom parameters.

Example: Taking the pagination of external data retrieval as an example, we often need to pull data in pages from an API. Some data interfaces do not return the total number of pages but instead indicate in the response whether there is a next page. In such cases, we can choose to use the "loop when conditions are met" method to retrieve all paginated data. For instance, the "DingTalk-Get User List" interface returns a "has_more" parameter to indicate if there is another page, and it also returns a "next_cursor" cursor to indicate the position for reading the next page. We can create a "loop when conditions are met" node as follows:

Create a text parameter named hasMore, initially set to true, and define the loop's exit condition as hasMore equals false. After saving, open the automatically generated loop process and configure it using a simplified flow to demonstrate the logic:

  • First, add a Call Integrated API node in the loop process and pass in the required parameters to the DingTalk API.

  • Then, add a Branch node to check the API response.

    If data is returned, proceed with the business logic.

    If no data is returned, update the hasMore parameter to false to exit the loop.

  • Next, determine whether another page exists.

    If yes, update the cursor parameter with the API's next_cursor value and continue to the next loop iteration.

    If no, update hasMore to false to exit the loop.

This example demonstrates that the core logic of “Loop when conditions are met” lies in dynamically updating workflow parameters to meet the defined exit condition. If the condition is never met, the loop will continue until it reaches the maximum loop count.

Update and use loop process parameters

Updating parameter values in a loop

Custom parameters within the loop process can be modified using the “Update Parameters” node.

System parameters such as index, start, end, and step cannot be modified through the “Update Parameters” node.

Using parameter values in a loop

After the loop process has completed, the subsequent nodes in the workflow can access and use the final values of the loop parameters.

Reuse loop process

If a loop process has already been configured and meets your requirements, it can be reused. In the loop node, select “Use Existing Loop Process”, then choose the corresponding app and the loop process to reuse.