For Each and While Loops
For Each
Use the For Each activity to create a loop that runs once for each item in a source array. For Each loops are one of the two types of loops available in Automation. This type of loop consumes an array of data and runs the content of the loop a single time for each item in the array. If you want to loop for an arbitrary number of times or while a condition is true, see the While Loop Help topic.
Note:
-
When naming the For Each activity, always specify the entity name which will be used in a loop (example: expand For Each to For each indicator)
-
You can stop a loop using the Break activity.
-
You can skip the rest of a single iteration of a loop or continue after a failure using the Continue activity.
Source Data
The source data for a For Each loop must be an array. This often comes from activities such as Read Table from JSON/Text/XML or Split String. To provide the source data for the loop, you must choose the array variable.
Column Variables
Within the For Each loop, you may want to use the values in the table columns. You can do this by using the variable browser to choose the Loop, Source Array, Items, and Column you want to use. Here is an example where the name column is used as a variable:
Set Observable State
Two variables are available that enable a content author to set the state of observables in their incident response workflows that are intended for use by playbook tasks.
In the Set Variables activity within a For Each loop, go to the activity properties, open the variable browser, search or navigate to choose Result Message (string) and Succeeded (true or false), and enter their values.
Usage
Complete the following properties to use this activity:
- Source Array - Click the Variable Reference icon to choose a variable.
While Loop
Use the While Loop activity to create a loop that runs based on the conditions you configure. A While Loop is one of the two types of loops available in Automation. This type of loop will continue running as long as its control condition is true. If you want to iterate through a list of objects in a table, see the For Each Loop Help topic.
Note:
Loop Control
A While Loop activity runs for as long as the control condition is true. This control condition functions the same way as a Condition Block in that its control condition is a logical statement. You can either:
- Set this condition to something that will always be true and use a Break activity to stop the loop.
- Use a loop control variable. When you use a loop control variable, you change the value of the variable when you want the loop to end.
Note: If you forget to stop a While Loop, it will keep running for quite a while before automatically stopping. If you are iterating through a list of items, it is usually better to use a For Each Loop.
Using a Loop Control Variable
Use the following steps to create a loop variable that you can change to stop the loop:
- Create a local variable using the Boolean data type and set its value to true.
- Add a While Loop activity and set its condition to check whether or not the local variable you created is equal to true.
- Inside the loop, when you are ready for the loop to end, you can either set the local variable to false or use a Break activity.