Add a Foreach Loop

When a robot performs the same actions for multiple items, add the actions to a foreach loop. A foreach loop instructs a robot to iterate through a collection of items, one at a time. Provide the items to iterate upon using a variable collection.

Use Cases

Consider an organization that needs to manually update several invoices every day. Within a robot, they create a foreach loop, which contains the actions for updating the invoices. The input for the robot is a variable collection, which contains the invoices that require updates.

For more details and for other use cases that use a foreach loop, see the following:

Prerequisites

A foreach loop requires a collection variable, which is an array of values. For example, a variable can contain the invoice numbers that require updates. Define the variable at any time, including while you're adding the foreach loop.

Add a Foreach Loop

Note:

You must use the low-code capabilities to add this logic to a robot. Keep reading for step-by-step instructions.
  1. Open the robot to edit.
  2. Add the logic to the robot.
    1. On the canvas, point to an action, and click +.

      A mouse cursor points to an action in the canvas and hovers over the plus sign button, which appears at the bottom of the rectangle that represents the action. A plus sign button also appears at the top of the rectangle, for adding an action after the current action.

      A menu of available actions appears.

    2. Select the Flow control tab.

      The Flow control tab is selected

    3. In the list, select foreach.

      A foreach action appears on the canvas, and the foreach panel appears.

  3. In the panel, enter a Name and Description for the action.
  4. In the Collection field, specify the data set that the foreach loop iterates on.

    You have the following options:

    • Use the data set from a collection variable.

      1. Click within the Collection field, and select Variables Variables.

        The Variables panel appears.

      2. Determine whether the variable that you need appears in the list. If not, create it. See Create a Variable. When creating a variable, ensure the following:

        • The variable must be a collection.
        • The properties within the variable must not be collections.
      3. Select the variable to assign the value to, and drag it to the Collection field.

    • Use the data from an output property that is a collection.

      1. Click within the Collection field, select More options ..., and then select Input Input.

        The Input panel appears.

      2. Determine whether the input property that you need appears in the list. If not, create it. See Create a Trigger's Input or Output.

        The property must be a collection.

      3. Select the input property to assign the value to, and drag it to the Collection field.

  5. In Iteration parameter, enter the name to give to every record in the collection.

    The Iteration parameter becomes a variable that you can reference in any action within the foreach loop.

    For example, if you chose an Invoice variable for Collection, you might enter Current_Invoice as the Iteration parameter.

  6. Determine whether to select Continue iteration on error:
    • When Continue iteration on error is selected and an error occurs while a robot instance works on a record, the robot instance records the error, stops performing any additional actions on the record, and moves on to work on the next record. For example, if the foreach loop contains a logger action, the robot instance doesn't complete the logging work for any records with errors.

      This approach follows a fail-safe methodology.

    • When Continue iteration on error is not selected and an error occurs while a robot instance works on a record, the robot instance records the error and stops all work on all records.

      This approach follows a fail-fast methodology.

  7. Click OK.
  8. Above the canvas, select Save.
Next, add actions that the robot needs to complete within the foreach loop. For example, you might start by adding the Log action so that you can record the information that is passed into the foreach loop. See Add a Log Action or Add an Action to a Robot.