Pre-General Availability: 2024-09-02

Add a Foreach Loop

When a robot must perform the same actions for multiple items, include the actions in 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 Case

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.

Prerequisites

A foreach loop requires a collection variable, which is an array of values. In the use case above, the variable contains the invoice numbers that require updates. You can 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.
    1. In the navigation pane, select Projects.
    2. Select the project name.
    3. In the left toolbar, select Robot Robot.
    4. In the Robots box, select the robot to open.

      The canvas appears.

  2. Add the logic to the robot.
    1. On the canvas, point to an action, and click +.

      A menu of available actions appears.

    2. Select the Flow control tab.
    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 Output 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 instance, 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. On the Pre Validate and Post Validate tabs, specify whether to complete any validation before and after the action.
  8. Click OK.
  9. 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.