Node - Loop
Feature Overview
A loop node allows for the repeated execution of a segment of a process. Users can set a specified number of repetitions for the loop execution of a process segment, and it also supports executing a process segment repeatedly based on the specific conditions, continuing until the stopping conditions are met.
Loop when conditions are met
The process will continuously loop through a segment until the specified parameters meet the stopping conditions, at which point the loop will terminate.
Loop a specified number of times
To specify the number of repetitions for the process, set the initial value, ending value, and step value for the loop. The loop will terminate when the initial value exceeds the ending value.
Processes that loop multiple times are strictly executed in series.
With each loop, the execution count of the workflow is incremented by one.
Loop a specified number of times
Application Scenario: Randomly select 10 questions from a pool of 100 questions to create a set of examination papers.
Implementation Approach: Through the [Get single data] node to randomly obtain one question, copy the question and relate it to the examination paper, and mark the original question as obtained. This process will be repeated 10 times, and once the loop concludes, the mark will be removed from the question pool.
Add a loop node
In the workflow, add a loop node and select "Loop a specified number of times".
Configure the loop node
Parameter settings
If selecting "Loop a specified number of times," there are three fixed system parameters: start, end, and step. Only the initial value can be configured; the parameter names cannot be deleted or modified.
- start: The value at which the loop begins.
- step: The increment value for start after each loop. For example, start=1, step=2, after the first loop is executed, start=3.
- end: The value at which the loop ends. The loop will terminate when the start value exceeds the end value, and the next loop is not executed.
In addition to the fixed parameters, custom parameters can also be passed in. For instance, in the aforementioned application scenario, a new examination paper record needs to be retrieved during each loop iteration, necessitating the passing of the examination paper record ID (exampaperid). Within the loop, this ID will be used to access the examination paper record.
Loop termination
In the "loop a specified number of times" method, the termination of the loop can solely be determined by comparing the start and end values.
Examples of execution times:
start=1, step=1, end=3. The loop is executed 3 times, and after the third iteration, the start value becomes 4, which is greater than the end value, thus ending the loop.
start=1, step=2, end=3. The loop is executed 2 times, and after the second iteration, the start value becomes 5, which is greater than the end value, thus ending the loop.
start=1, step=1, end=0. The loop is executed 0 times.
Maximum number of loops
Looping cannot be performed indefinitely; the loop node supports a maximum of 10,000 iterations. When this limit is reached, the loop will automatically terminate and proceed to the subsequent node.
During the execution of the loop, even if the start value is less than the end value, the loop will terminate if the maximum number of iterations is reached.
If the process is interrupted during a loop
When an interruption occurs in the execution of a loop, there are three options for continuation:
Skip and move on to the next loop: Ignore the current interruption and proceed to the next iteration of the loop.
Skip and terminate the loop, continuing with subsequent processes: This option ends the entire loop and proceeds to execute the actions that follow the loop node.
Terminate the process: Directly terminate the process in which the loop node is located.
Configure the loop process
Click the button in the loop node to enter the looped process, and configure the actions within the looped process.
Configuration of the loop process is as follows:
The process will be executed 10 times, with a new question randomly selected after each execution. Normally, the start value will increase by 1 after each execution, proceeding to the next loop. When the 10th execution is completed, the start value will become 11, and the loop will end.
If there are only 6 questions in the question bank, which is less than 10 questions, on the 7th loop execution, no question will be retrieved. In this case, add the "abort the process" node to directly terminate the process and end the loop.
Complete the entire workflow
After the loop ends, clear the mark indicating questions that have been retrieved. The configuration of the entire workflow is completed.
Loop when conditions are met
In the "loop when conditions are met" method, there is a fixed system parameter, "index". Each time an execution is completed, its value automatically increments by 1. Whether or not to stop the loop can be determined by 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:
We added a text parameter "hasMore," initially set to true, and defined the "loop termination condition" as "hasMore equals false." After saving, open the automatically created new loop process. Let's illustrate the loop with a simplified process:
In the loop process, start by adding a "Call Integrated API" node and passing the parameters to DingTalk's API.
Next, add a branch node to evaluate the result of the API request. If there is data, carry out the necessary business processing; if there is no data, update "hasMore" directly to "false" and end the loop. Then, check if there is another page. If there is, update the value of "cursor" to the "next_cursor" value returned by the interface and continue to the next loop; if there is no next page, update "hasMore" to "false" and end the loop.
It can be seen that the core logic of the "loop when conditions are met" method is to modify the values of "process parameters" to meet the defined stop conditions within the loop, thereby terminating the loop. Otherwise, the loop will continue until the "maximum number of loops" is reached.
Update and use process parameters in the loop
Parameters can be custom-modified in the process, for instance, the start parameter in the "loop a specified number of times" method can be changed through the update parameters node. If not deliberately modified, it will increase according to the step value.
Reuse loop process
If the required loop process has already been configured, it can be directly reused. In the loop node, select "Use existing loop process," then choose the application and the target loop process.
Have questions about this article? Send us feedback