The Looped Actions Action runs a sequence of other Actions, once for every iteration of the loop. This can be handy when you have a variable number of tasks you want to perform. You must define an iterator and a Condition to specify when the loop should end.
There are three essential parts to configuring a Looped Action:
-
Condition: Defines how many times your loop should run. An example condition would be
Selected Index < Custodian List
Size. This Condition will tell the loop to run as long as the selected index (or iterator) is less than the custodian list size. - Iterator Action: In most cases, your iterator Action should be an Update Transaction Action that increments an integer Field in your App. The iterator must be incremented each time the loop runs, or the loop will not break and will hit the max loop limit of 999. In addition, you’ll usually want to create a separate Update Transaction action that resets your iterator back to zero. This action will run (only once) immediately after your loop starts.
- Looped Actions: The sequence of Actions that will be run for every loop iteration. These Actions will run in the order you specify. One of these Actions should be your Iterator.
This tutorial will cover setting up all the items listed above.
Create Iterator Field
While loops in programming languages like Java and Python automatically keep track of iterators, you must increment your Onit loop manually.
In most cases, you will create a hidden Integer Field in your App to set up an iterator.
- Navigate to your App’s Wizard.
- Create a new Field of type Integer.
- Set an appropriate Initial value for your iterator. The Initial value will almost always be zero.
-
Type
true
under Hidden Condition.
- Save your changes.
Create Iterator Actions
Now that you have created a Field to hold your iterator, create an action to increment it.
- Go to your app's Actions node and create a new Update Transaction Action. This Action will increment the integer in your iterator Field.
-
Under Field, choose your iterator Field. In this example, the field is named
processing_index
. -
Under Liquid enter
{{ iterator_field_name | plus: 1 }}
. This Liquid will take the integer and add one. If you want to increment by a different value, such as two, specify it here.
- Click Ok to save your Action.
While still in the Actions node, we will create an Action to set the iterator back to zero. This Action will be used to clear your iterator before using it in another loop.
- Create another Update Transaction Action and choose your iterator Field from the Field dropdown.
-
In Liquid, enter
0
. If you want to reset the integer to another value, specify it here.
- Click Ok to save your Action.
Writing Conditions for Looped Actions
When creating a Condition for your looped Action, you should think about how many times you need your loop to run. Your condition will often be checked against the length of a list or a group of Records. The structure of a simple Condition is as follows:
{% if iterator_value < value_to_check_against %} true {% else %} false {% endif %}
Once your Condition evaluates to false
the loop will break. If your Condition never breaks, Onit will kill the loop at the user-specified Max Loops or the system max of 999 iterations to prevent an infinite loop. Here is an example of a Condition:
Configuring a Looped Action
You’re finally ready to specify which Actions should run inside your loop.
- Navigate to the Actions node in your App and create a new Looped Actions Action.
- Under Initial Actions, choosing your “set iterator to zero” Action is often recommended. That said, this may not make sense for all use cases.
- You must specify a value for Max Loops. If you know your loop should not reach the system max of 999 iterations, then you should specify a lower number to prevent a runway loop.
- In the Loop Condition dropdown, select the appropriate condition.
- Enter your Actions in the Looped Actions dropdown in the order they should run.
It is essential to include your iterator Action within the Looped Actions.
- Click Ok to save your looped Action.
Test Your Looped Action
You can just run your looped Action to make sure that each Action is run for each iteration of the loop. You can look in the Debugger/Logger to ensure every Action is being run. Ensure your App is not running slowly; this could be a sign of a loop running until the max loop limit is reached.