You can insert a Foreach element in the workflow that you develop to run a subworkflow that iterates over arrays of parameters or variables. To improve the understanding and readability of the workflow, you can group several workflow parameters of different types that are logically connected in a single type that is called a composite type.
Using Foreach Elements
A Foreach element runs a subworkflow iteratively over an array of input parameters or variables. You can select the arrays over which the subworkflow is run, and can pass the values for the elements of such an array when you run the workflow. The subworkflow runs as many times as the number of elements that you have defined in the array.
If you have a configuration element that contains an array of variables, you can run a workflow that iterates over these variables in a Foreach element.
For example, suppose that you have 10 virtual machines in a folder that you want to rename. To do this, you must insert a Foreach element in a workflow and define the Rename virtual machine workflow as a subworkflow in the element. The Rename virtual machine workflow takes two input parameters, a virtual machine and its new name. You can promote these parameters as input to the current workflow, and as a result, they become arrays over which the Rename virtual machine workflow will iterate. When you run your workflow, you can specify the 10 virtual machines in the folder and their new names. Every time the workflow runs, it takes an element from the array of the virtual machines and an element from the array of the new names for the virtual machines.
Using Composite Types
A composite type is a group of more than one input parameter or variables that are connected logically but are of different types. In a Foreach element, you can bind a group of parameters as a composite value. In this way, the Foreach element takes the values for the grouped parameters at once in every subsequent run of the workflow.
For example, suppose that you are about to rename a virtual machine. You need the virtual machine object and its new name. If you have to rename multiple virtual machines, you need two arrays, one for the virtual machines and one for their names. These two arrays are not explicitly connected. A composite type lets you have one array where each element contains both the virtual machine and its new name. In this way, the connection between those two parameters in case of multiple values is specified explicitly and not implied by the workflow schema.
Define a Foreach Element
If you want to run a subworkflow multiple times by passing different values for its parameters or variables in every subsequent run, you can insert a Foreach element in the parent workflow.
When you insert a Foreach element, you must select at least one array over which the Foreach element iterates. An array element can have different values for each subsequent workflow run.
If the subworkflow has output parameters, you must select the output parameters of the Foreach element in which to accumulate workflow outputs, so that the subworkflow can iterate over them as well.
Procedure
Results
You defined a Foreach element in your workflow. The Foreach element runs a workflow that takes as parameters every element from the array of parameters or variables that you have defined.
For parameters or variables that are not defined as arrays, the workflow takes the same value in every subsequent run.
Example: Rename Virtual Machines by Using a Foreach Element
You can use a Foreach element to rename several virtual machines at once. You have to insert a Foreach element in a workflow and promote the vm and the newName parameters as input to the current workflow. In this way, when you run the workflow, you specify the virtual machines to rename and the new names for the virtual machines. The virtual machines are included as elements in the array that you created for the vm parameter. The new names for the virtual machines are included in the array that you created for the newName parameter.