Creating Loops within Onit Workflows

by Christina Moore Updated Aug 19, 2019

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 that 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:

  1. 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.
  2. 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 that the loop runs, or the loop will not break and will hit the max loops 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 up.
  3. Looped Actions: The sequence of Actions that will be run for every iteration of the loop. These Actions will run in the order you specify. One of these Actions should be your Iterator.

In this tutorial we will cover setting up all of 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.

To set up an iterator, in most cases you will create a hidden Integer Field in your App.

  1. Navigate to your App’s Wizard.
  2. Create a new Field of type Integer.
  3. Set an appropriate Initial value for your iterator. The Initial value will almost always be zero.
  4. Type true under Hidden Condition.
  1. Save your changes.

Create Iterator Actions

Now that you have created a Field to hold your iterator, create an action to increment it.

  1. Go to the Actions node of your App and create a new Update Transaction Action. This Action will increment the integer in your iterator Field.
  2. Under Field choose your iterator Field. In this example the field is named processing_index.
  3. Under Liquid enter {{ iterator_field_name | plus: 1 }}. This Liquid will take the integer and add one. If you would like to increment by a different value, such as two, specify it here.
  1. Click Ok to save your Action.

While we are 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.

  1. Create another Update Transaction Action and choose your iterator Field from the Field dropdown.
  2. In Liquid enter 0. If you would like to reset the integer to another value, specify it here.
  1. 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 checking 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 of your loop. 

  1. Navigate to the Actions node in your App and create a new Looped Actions Action.
  2. Under Initial Actions it is often a recommended that you choose your “set iterator to zero” Action. That said, this may not make sense for all use cases.  
  3. 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.
  4. In the Loop Condition dropdown select the appropriate condition.
  5. 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.

  1. Click Ok to save your looped Action.

Test Your Looped Action

Run your looped Action to make sure that each Action is run for each iteration of the loop. To make sure every Action is being run, you can look in the Debugger/Logger. Check to make sure your App is not running slowly, this could be a sign of a loop running until the max loops limit.

 

Previous Article Collecting Form Submissions from Anonymous Users
Next Article Adding a Participant from a Field Value

© 2020 Onit, Inc.

docs.onit.com contains proprietary and confidential information owned by Onit, Inc. that is subject to copyright. Onit presents it exclusively to you for your sole use in conjunction with using Onit products. No portion of the materials contained herein may be used for any other purpose. No portion of the materials contained herein may be shared with third parties or reproduced in any form.