Loop - Examples                                      How to use?      Tips

 

Example 1: To open 5 Internet Explorer windows with one click.

  1. Open the Task Editor from Tools->Task Editor.
  2. Double-click on Loop command and select the option 'Times' and write 5 in the text box.
  3. Save the command.
  4. It automatically inserts End Loop command along with it.
  5. Insert an Open Progam/File command to open IEXPLORE.exe within the Start and End Loop command.

Example 2: Loop through a list and insert one value at a time in an already open excel document.

  1. Create a list variable from Tools->Variable Manager and let's call it $my-list$.
  2. Specify an option to have the variable read from a text file.
  3. Let's say you have filled the values of the list variable.
  4. Double-click on Loop command.
  5. Select 'List' option and select $my-list$ from the drop down list.
  6. It will insert Start and End Loop commands in the editor.
  7. Insert a Keystroke action $my-list$[ENTER in the Excel window. It will type in the next value of the list variable every time it loops.

Example 3: Monitor an application, send email to administrator if the application shuts down.

  1. Double-click on Loop command.
  2. Select 'Condition' option.
  3. It will open a new window. In that select 'Application Running' option and specify the path of your application that you want to monitor for e.g. C:\My-Application.exe
  4. Save the command. It will insert a Loop-End Loop commands in the editor.
  5. Insert a Send Email command after the End Loop command.
  6. When you run this task, it will keep on looping if the application is running. As soon as the application crashes or shuts down, it comes out of the Loop and sends an email alert.

Example 4: Query for all the contacts in the database that have the lastname beginning with 'R' and print the names in Excel document.

  1. Using Database commands connect to a database and issue a SQL Query to fetch your records. Insert Connect command first then SQL Query command.
  2. Double-click on Loop command.
  3. Select 'Each row in a SQL Query dataset' option.
  4. To access the result returned by the SQL Query, use the $Dataset Column$ System variable provided by Automation Anywhere. Use Loop for Each row in a SQL query dataset option to loop through all the rows of the SQL Query result. To access each column use $Dataset Column(index)$ for e.g. to get the first column use $Dataset Column(1)$ and so on.
  5. You can use Insert Keystrokes commands with $Dataset Column$ variable to type the results in the Excel document.
  6. Disconnect from the Database.

Example 4: Print all the prices imported from a pricing table from a web page.

  1. Use the Web Recorder 'Import Table' command to import the tabular data from the web page.
  2. After you call the Import Table command, Automation Anywhere stores all the tabular data in a System variable called $Table Column(index)$. You can use this variable to get all the rows and columns of the table that you imported. 
  3. Double-click on Loop command.
  4. Select 'Each row in an Internet Explorer Table' option.
  5. To access the each row and column in the $Table Column$ System variable provided by Automation Anywhere use Loop for Each row in an Internet Explorer table option to loop through all the rows of the table. To access each column use $Table Column(index)$ for e.g. to get the first column use $Dataset Column(1)$ and so on.
  6. You can use Insert Keystrokes commands with $Table Column$ variable to type the results in the Excel document.

Example 5: Insert an 'Exit Loop' to break out of the loop.

  1. Open the Task Editor from Tools -> Task Editor.
  2. Double click on the Loop command and select 'Exit Loop' option and move it to the appropriate location between the Start and End Loop commands.
  3. Exit Loop command can appear only between Start and End Loop.

 

Tips for Using Nested Loop

Let's say you are using the Nested Loop as follows

1 Start Loop " List Variable my-list-variable"
2     Variable Operation : $my-list-variable$ To $Income$
3     Start Loop "5" Times
4         Keystrokes: $Income$[ENTER] in "Untitled - Notepad"
5     End Loop
6 End Loop

You should assign the value of the list variable to a value type variable before the nested loop starts and then use the value type variable inside the nested loop.

You can apply the same tip when you are using Loop for each row in an Internet Explorer Table, Loop for each row in a SQL Table, Loop for each row in an Excel dataset.