For Each
Cycles through a collection and assigns the value of each item in it to a variable. The variable's value is replaced on each iteration.
Command availability: IBM RPA SaaS and IBM RPA on premises
Script syntax
IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.
foreach --collection(List<Variant>) --variable(Variant) [--distinct(Boolean)]
Input parameters
The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.
Designer mode label | Script mode name | Required | Accepted variable types | Description |
---|---|---|---|---|
Collection | collection |
Required |
List<Any>, Stack<Any>, Queue<Any>, String Dictionary<Any>, Email Connection, Error, Text File Reader, Workflow Batch |
Collection cycled through |
Variable | variable |
Required |
Any |
Variable to use in the iteration. |
Distinct | distinct |
Optional |
Boolean |
Option used to assign repeated values within the Collection to the Variable only once. |
Example
Example 1: Cycles through a Queue collection and displays the values assigned to the variable during the execution, with the Distinct option enabled.
defVar --name collectionCycledThrough --type Queue --innertype String --value "[a,b,c,a]"
defVar --name valueCycledThrough --type String
foreach --collection "${collectionCycledThrough}" --variable "${valueCycledThrough}" --distinct
logMessage --message "${valueCycledThrough}" --type "Info"
endFor
//Result:
//a
//b
//c
Example 2: Similar to the previous example, but this time the Distinct option is disabled.
defVar --name collectionCycledThrough --type Queue --innertype String --value "[a,b,c,a]"
defVar --name valueCycledThrough --type String
foreach --collection "${collectionCycledThrough}" --variable "${valueCycledThrough}"
logMessage --message "${valueCycledThrough}" --type "Info"
endFor
//Result:
//a
//b
//c
//a