Use child job looping in workflows

Itential has reverted the deprecation of the forEach task, allowing it to remain in the platform. As a best practice, Itential recommends using a child job loop.

In workflow design, there are scenarios where a childJobLoop is preferable to a forEach task. Using childJobLoop improves workflow readability and promotes modularity by centralizing looping logic rather than duplicating it across multiple workflows. It also provides a cleaner way to run sequential and parallel loops through a child job task.

TaskDescriptionRecommendation
forEachExecutes a loop inside a job by iterating over each item in an array.Replace with child job using a Loop Type property.

Convert a forEach task to a child job loop

The main components of the conversion are:

  • Moving the looped task into its own child workflow.
  • Replacing the forEach task with a child job task that iterates over the array.
  • Mapping the input and output variables of the child job.
1

Create a child workflow

Create a new workflow containing only the tasks that were being looped in the forEach task. Cloning the original workflow and removing non-looping tasks is often the easiest approach.

2

Replace the forEach task

In the original workflow, remove the forEach task and replace it with a child job task. Set the Workflow field to the child workflow created in the previous step.

3

Set the loop type

Set loopType to sequential or parallel.

4

Set the loop array

Set loopArray to the referenced dataArray. Ensure that the items in the array are objects.

Pass inputs directly to a child job (basic example)

Parent workflow

In the starting workflow, the forEach task iterates over an array [1,2,3]. For each iteration, the task makes a REST call to an API endpoint.

Parent workflow with forEach task iterating over an array

Create the child workflow

Create a child workflow for the task being looped. Clone the original workflow and leave only the looping tasks.

Child workflow containing only the looping tasks

Replace forEach with child job

In the parent workflow, remove the forEach task and replace it with childJob.

Parent workflow with child job task replacing forEach

Set the loop type

Open the task details for the child job task. Update the Workflow, Loop Type, and Loop Array fields. Use Sequential for Loop Type. Click Save.

child job task details showing Workflow, Loop Type, and Loop Array configuration

Convert array to objects

The values passed to the child job task must be objects. Open the New Job Variable in the parent workflow and change the data_array input to [{},{},{}]. Click Save.

New Job Variable updated with array of objects

Run the workflow

Click Run to execute the workflow and confirm the conversion is complete.


Use transformations to pass inputs to a child job (advanced example)

This example builds on the basic example above and demonstrates using a JST to map loop array values to child job inputs.

Restore the parent workflow

Open the New Job Variable and change data_array back to "[1,2,3]". Click Save to restore the original state.

Create a JST

Open the task details for the child job task. Click the + icon next to the Transformation field to create a JST that maps the Loop Array to input values for the child workflow.

Creating a JST from the child job task details panel

Set the outgoing schema

On the JST page, set the outgoing schema with an id of num and a type of integer. This allows the data_array variable to be passed to the outgoing schema using the original [1,2,3] input array. Save and close the JST.

JST outgoing schema configured with id: num, type: integer

Configure the viewData task input

Open the task details for the viewData task. For the message variable, change Reference task to job and Reference variable to message. Click Save.

viewData task with message variable referencing job.message

Use the JST to pass $id of message instead of num. This creates the object array [{"message":1},{"message":2},{"message":3}] that is passed to the child job task.

JST mapping message id to produce the loop array as objects

Save the transformation, return to the parent workflow, and click Run. After completing each child job, the Rest Call Output is visible.

Rest Call Output after running the parent workflow with child job

Capture child job task output

If the parent workflow requires the output of the child job task, use the output data from job_details. Add a viewData task and configure it to receive job_details as input.

viewData task added to capture child job job_details output

Pass the job_details output to the body variable.

job_details passed to the body variable

Run the parent workflow and work through each child job’s viewData task. The job_details of the child job task appear as output per iteration.

job_details output for each child job iteration

Note that no job variables may be visible initially. To get the REST API call output, open the child job in designer mode, select the RestCall task, and check the Job Variables box under Outgoing.

Enabling Job Variables output on the RestCall task in the child workflow

After running the parent workflow again, the job_details output for each child job iteration includes the REST call results.

Complete job_details output for each child job iteration including REST call results