The forEach task executes a loop inside a job. It iterates over a passed array, and for each element, it executes a series of tasks. The forEach task is helpful when using tasks that operate on one thing, such as a device, but then you need to repeat those steps over a set of devices. Building a loop in the workflow is more efficient than writing new tasks.
The forEach task is unique as it supports transitions that follow the loop value. When a transition is created to follow the loop, it is distinguished with a blue arrow. The series of tasks at the end of the blue arrow then dictates what will be executed for every iteration of the forEach task. A forEach task also supports transitions bound to the success and error finish states. When forEach completes its iterations, barring any errors it returns with a success finish state, the job then follows the transitions bound to that finish state.
||Specify the array of items forEach will use in its iterations. The value can come from a job variable, a static value, or an earlier task's outgoing variable. If the reference task is a job variable or earlier task, the reference variable shows a dropdown of available variable names for selection. If the reference task is static, the reference variable becomes a text edit field.|
||In each iteration, the forEach task copies the next
Replace Retired Device Group
Use the forEach workflow operation task for a job that replaces a retired device group used on every device with a new group.
When the job begins, the first automatic task returns a list of all devices. The device list is passed to a forEach task with executes four tasks for every device passed. The four (4) tasks in the example above remove each device from the retired device group, adds each device to the new device group, then concurrently gets any configuration on the device not in sync with its modeled configuration while getting all groups for the device. After the last device is processed, the forEach task returns with a success finish state, and the job completes.
Handling Errors Inside forEach Loops
Use the following to handle errors inside forEach loops.
- Create newVariable.
- Name variable as
- Inside the forEach loop, assume you want to track errors for generic "task X".
- Create a push task to transition
saved_errorsfrom "task X" to the push task.
- Push the errors into the array.
- Query the job variable and evaluate if the length is 0 or not.
![Handle Errors forEach loop](image/Handle-Errors-Inside-forEach-Loops.png