Iteration is a powerful component of the OneCloud platform. An iteration can loop over a fixed or dynamic list of values using the List type. This is useful for processing Outputs such as those created by Advanced Query or lists stored as Workspace Resources.

There are times when an iteration needs to be more dynamic and loop until a certain condition is met. Let's consider an HTTP request that returns multiple pages of information. In one example, the response includes a key that specifies the number of total pages in the response. In this example, a For loop is needed to iterate over all of the pages of the response.

In a second example, the HTTP response does not include the total number of pages but instead has a key on each page indicating if there are more pages. In this example, a Do loop is needed to iterate over all of the pages of the response.

ℹī¸ This article provides instruction on how to construct various Repeat Until loops. These are based on the above example and may need to be modified to address an organization's specific needs.


For Loop

A For loop is used when the exact number of iterations is known. For loops often utilze a numeric counter. In a code based view they are often represented like the below:

For counter = start_value to end_value
'Loop actions
Next

Creating a For loop in OneCloud is easy. First, create a Dynamic Chain Variable. We suggest naming this Variable Counter. Do not set an initial value for the Variable.

Prior to the Command Group that will be used to create the For loop, add a Set Dynamic Chain Variable Event. Configure the event to set the initial value for each execution of the Chain. Using the HTTP example discussed above, the HTTP request that we need to make would start with a page counter of one (1) so we initialize the Variable with this value.

Add a Command Group to the Chain. Make sure the Set Dynamic Chain Variable Event is connected to the In section of the Group. Select the Iterations tab and enable iterations using the toggle. Select Repeat Until from the Modifier type. In the rules section, add a Rule that specifies the upper limit of the For loop.

Continuing with the HTTP example, our response includes a key named Total_Pages. The For loop needs to repeat until the Counter variable, which starts at one, is greater than the total number of pages in the response. The Counter is incremented as a step within the Group meaning that the Counter value will grow with each iteration of the For loop. A Parse Number Transformation is applied to the Counter Variable to ensure that the Variable is treated as a number.

Within the Command Group, add the Commands that require an iteration. The last node within the Command Group should be a Set Dynamic Chain Variable Event. This Event is used to increment the Counter Variable.

The configuration of the Set Dynamic Chain Variable Event is applicable to any For loop approach. In the Value parameter, select the Counter Dynamic Chain Variable.

Click the Variable to apply a Transformation. First apply the Parse Number Transformation since Dynamic Chain Variables are stored as string/text. Second, assign the Add Math Transformation and set the value to a numeric one (1). Accept the Transformations and save the Event.

An example Chain execution using a For loop representing the HTTP example outlined above is below. In this example, 22 pages were found in the initial HTTP GET request so the For loop executed 22 times.


Do Loop

A Do loop is used when the total number of iterations needed is unknown but instead the loop must continue until one or more conditions are met. There are two primary types of Do loops, Do While and Do Until. Both accomplish the same objective using different approaches. In a code based view they are often represented like the below:

Do While

Do While some_condition
'Loop actions
Loop

Do Until

Do Until some_condition
'Loop actions
Loop

Creating either Do loop is easy in OneCloud. First, create the Dynamic Chain Variables needed to support the process.

In the HTTP example above, the response includes a has-more key that indicates if additional pages are available. The HTTP request URL includes a parameter to specify the offset (i.e., page) to retrieve. This offset is incremented as part of Do loop.

For this example, two Dynamic Chain Variables are needed. The Condition Test Variable is used to store the has-more key from the HTTP response. The Offset Variable is used to dynamically update the HTTP GET URL in the loop.

Prior to the loop, the Set Dynamic Chain Variable Event is used to reset the Dynamic Chain Variables for each execution of the Chain.

In the Command Group, enable Iteration. Set the iteration type to Repeat Until and then specify the condition(s) that need to be evaluated to determine if the iteration should repeat. The Commands within the Command Group will be executed in a loop until the iteration Repeat Until condition is met.

In this HTTP example, the loop needs to execute until the has-more key is false.

Within the Command Group, add the Commands that require an iteration. The last node within the Command Group should be a Set Dynamic Chain Variable Event. The configuration of the Set Dynamic Chain Variable Event is applicable to any Do loop approach.

In this example, the Condition Test Variable is updated with the results of the HTTP GET request response. The Offset Variable is incremented using Math Transformations (Parse Number, Add) to enable the HTTP GET request to retrieve the next page of data.

Math Transformation

An example Chain execution using a Do loop representing the HTTP example outlined above is below. In this example, the loop executed three times before HTTP GET request has-more key returned a value of false.


📚 Topics covered in this article

Did this answer your question?