To run a command or command group in a chain repeatedly, set up an iterator.
Note: To convert an output from a relational database or external web service to a JSON array to use with an iterator, use the JSON connector's CSV to JSON command or a JSON variable transformation.
Set up an iterator
With an iterator, you can repeat a command or group based on:
- A static or dynamic list of values
- An exact number of values
- Based on whether a condition exists
An iterator can repeat a command or group over:
- A static list of values, such as regions or entities
- A dynamic or variable list of values, such as from a file uploaded as a workspace resource or output from a command earlier in the chain
Note: A command or group is limited to 1000 iterations.
To run an iterator over a list of values:
- In the chain, select the command or group to repeatedly run, and click Edit .
- On the Iterations tab, enable Iterator .
- In Select modifier type, select List.
- Enter the iterations' values:
- For a static list, enter the value for each iteration, pressing Enter after each.
- For a dynamic list, select the output from the earlier command with the values to use.
- For a static list, enter the value for each iteration, pressing Enter after each.
An iterator can repeat a command or group over an exact number of values, such as a total of pages output from a command earlier in the chain.
Step 1. Set up a variable for the number value
To indicate the number of iterations, create a dynamic chain variable:
- In the chain, add a dynamic variable for the number of iterations, such as Counter, with no initial value.
- To set the variable's initial value when the chain runs, after the command that outputs the number of iterations, add a Set dynamic chain variable event:
- Select the dynamic variable created for the number of iterations.
- In Value, and enter its starting value, such as 1.
Step 2. Add a command group
To indicate the commands to iterate and the iteration rule, add a command group:
- After the Set dynamic chain variable event, add Command group, and drag a link between them.
- Select the group, and click Edit.
- On the group's Iterations
- In Select modifier type, select Repeat until.
- Click + Rule, select a data type of Float, and specify when to stop iterations. For example, to iterate based on the Total records output of an earlier command, set a rule to stop iterations when the variable value is greater than (>) the Total records output.
- Add the commands to iterate to the group.
Step 3. Add an event to increment the variable value
To increment the variable value with each iteration:
- As the last step of the group, add another Set dynamic chain variable event:
- Select the dynamic variable created for the number of iterations.
- In Value, select the same variable.
- To transform the value, click the variable in Value, and add a Parse Number transformation.
- Add an Add transformation with a value of 1, and click Accept.
If the values or exact number of values are unknown, you can iterate based on whether specific conditions are met. For example, you can repeat a command while or until specified criteria exists.
Step 1. Set up variables for the conditional values
To represent the conditions that indicate values to iterate over, create dynamic chain variables. For example, if a command provides a boolean key output to indicate whether its response contains additional pages and an integer offset parameter to identify each page, create variables for these values:
- In the chain, add a dynamic variable for each condition, with no initial value.
- To set the variables' initial values when the chain runs, after the command that outputs the conditions' values, add a Set dynamic chain variable event, select the dynamic variables, and enter their starting values.
For example, enter the boolean key's value as True, and the offset value as 0.
Step 2. Add a command group
To indicate the commands to iterate and the iteration rule, add a command group:
- After the Set dynamic chain variable event, add Command group, and drag a link between them.
- Select the group, and click Edit.
- On the group's Iterations
- In Select modifier type, select Repeat until.
- Click + Rule, and specify when to stop iterations, such as when the boolean key is False.
- Add the commands to iterate to the group.
Step 3. Set variable values with each iteration
As the last step of the group, add another Set dynamic chain variable event to set or increment the variable values with each iteration.
- To set variable values, map each variable to their respective outputs from the earlier command. For example, for the Boolean variable, select the boolean key output; for the Offset variable, the integer offset parameter.
- To increment a variable, such as Offset, click its value, and then apply a Parse Number transformation and an Add transformation with the value to increment with each iteration.
Iterator outputs
You can use an iterator's output as a property for other commands or events later in the chain. The outputs depend on the iterator' modifier—List or Repeat until.
Output | Description |
---|---|
Command iteration or Group iteration | The variable value of the iteration processed in the loop |
Index | The zero-based count to indicate the iteration processed in the loop |
First iteration |
A boolean value to specify whether the iteration processed is the first in the loop |
Last iteration | For a List iterator only, a boolean value to specify whether the iteration processed is the last in the loop |
Total iterations | For a List iterator only, a zero-based count of the total iterations processed by the loop |
To use an output as a property for another command or event, select it under Command Iterator or Group Iterator, respectively.