Working with loops in RPAs

Add a loop to repeat steps for every file, row, email, record or a set number of times, filter what it loops over, and connect it so the process carries on when it is done.

Help Centre › Building Processes

A loop repeats a group of steps once for each item: each file in a folder, each row in a spreadsheet, each unread email, and so on. Each time round, the current item is put in the loop variable you name, so the steps inside can use it. If you are new to process maps, read Working with process maps first.

1. Add a loop

  1. On the process map, open the Add step tab, type "loop" in the search box and drag the kind of loop you want onto the canvas, for example For each file in folder. The step form opens:
    {{popups:rpas/add:action=loop}}
  2. Give the loop a name.
  3. Fill in what it loops over, such as the folder path.
  4. In Loop variable, type a name for the current item, for example file. Inside the loop you then read it as {file::name}, {file::path} and so on. See Loop variables.
  5. Optionally add filters and a Maximum loops limit.
  6. Click Add.

You can also copy an existing loop and edit it.

The Add step tab also lists Break out of loop, Continue to next iteration and Schedule loop instance. The bot does not run these yet: a run that reaches one stops with an error. To skip an item, use a filter, or a Decision whose path goes straight back to the loop step.

2. Kinds of loop

LoopRepeats for
For each row in Excel sheetEvery row of a sheet. Give the file path, the sheet, the first data cell (top left of your table) and whether the data has a header row. With a header row, each column can be read by its heading.
For each sheet in Excel fileEvery sheet (tab) in a workbook. You can include hidden sheets.
For each file in folderEvery file in a folder. Turn on Include sub-folders to go into folders inside it.
For each subfolder in folderEvery sub-folder in a folder.
For each record in JSON fileEvery record in a JSON file.
For each record from APIEvery record an earlier API step returned.
For each record in listEvery record in a list you already hold in a variable, such as the lines of an invoice: invoice::line items. See Working with invoices.
For each item in listEvery item in a piece of text split by commas or new lines, such as Alex, Ben, Carla.
For each row in web tableEvery row of a table on a web page.
For each emailEvery email in an Outlook folder: unread ones unless the loop's Read state, importance, subject, sender or attachment filters say otherwise.
Repeat number of timesA fixed number of times.
While condition is trueAs long as a condition stays true. Make sure it will become false at some point, or set Maximum loops, otherwise the loop never ends.

3. Filter what the loop takes

Add filter rows to skip items you don't want. For example, to loop only over Excel files in a folder, with the loop variable file:

  • {file::extension} = .xlsx, or
  • {file::path} endswith xlsx

For a spreadsheet with a "Name" column, to keep only rows whose name contains "An": {Name} contain An.

Match says how several rows combine: all of them, any of them, none of them, or exactly one. The operators are listed in Working with conditions. Use the exact field name: a row that names a field which does not exist fails.

4. Connect the loop

A loop step needs one connection in and two out:

  • DEFAULT: to the first step to repeat. Connect the last step to repeat back to the loop step.
  • FALSE: to the step that runs once every item has been done.

The map makes a step's second connection EXCEPTION (red). Click it and change it to FALSE. A loop can't have two connections of the same type.

5. Tips

  • Turn on Marks a transaction to report each item separately, and set a Transaction key such as {file::name} so you can tell them apart.
  • Keep the steps inside a loop few and quick. The loop runs them for every item.

Get started

See it working on your own data

Everything documented here ships with the platform – try the document tools free, or have your first automation working in 7 days.