Loop over an array using a group node


In this tutorial, we'll go through the process of handling an array to a group node and using it to create a sub workflow that processes each element of the array from a parent flow inside a child flow.


Pre-requisites

Before you begin

Ensure you have:

  • A node setup in your workflow that outputs an array, in this example we will use an Ocean search node.
  • A clear understanding of the sub workflow you want to apply to each element of the array.
Cover image

## Retrieve the array from an Ocean search node

Run an Ocean node with the right filters to output an array of companies similar to the domain provided



Step 2

Add a group node to the workflow

Add a group node to your workflow from the node catalog.


Pass the array from the Ocean search node to the group node.

Pass the array of persons to the group node

{{ nodes.oceanio_search }}


Step 3

Configure the child workflow in the group node

Imagine the group node as a container that processes each element of the array individually, similar to how a loop operates in traditional programming.


Execute the group node to ensure that the the child workflow has input data to build context for the start node contained inside.

Click on the expand button on the bottom-right of the group node to enter the child workflow that will handle each element.

This child workflow can include any combination of nodes that you need to process the array elements.

For example, you can add nodes to send an email, log data, or perform transformations on each element of the array.




When an array is passed into the child workflow, each element of the array is available as an input to the child workflow using the start node of the child workflow.

Add an expression referencing the company domain field inside each item of the array

{{ nodes.start.domain }}

However, if you need access to any elements in the parent workflow, which spawned the child workflow, you have to use the parentNode object from within the child workflow.

Reference the hubspot_owner_id of the company available in the parent workflow

{{ parentNodes.start.hubspot_owner_id }}


## Execute the group node


- Run the group node: With the sub workflow configured, execute the group node. The output will contain the processed results for each element in the array. - Review the output: Check the results to ensure that each element of the array has been processed as expected by the sub workflow. - Review the execution inside the group node: When on the Runs view of the workflow, clicking a certain run and then expanding the group node will let you cycle through the runs of each of the array's items in the child workflow.


Once the group node has finished successfully executing, it will output an array containing the output of the last node in the 'child workflow' for all the executions. This array can be used in the parent workflow to further process the data or be sent to another group node.

For instance, if you had chosen to enrich each of the domains passed by the Ocean node for the relevant stakeholders, you could output an array of these stakeholders from the first group node and process this array inside a second group node.

As the input of the second group node, you will require Javascript's flat method to flatten the array of arrays into a single array using an expression as follows:

Flatten the array of arrays into a single array

{
  {
    nodes.group1.flat();
  }
}

This flattend array can then be passed to the second group node for further execution. This will create a child workflow inside this second group node. Each element of the flattened array will be processed by this second child workflow. In our example chosen above the second child workflow could be used to send an email to each of the stakeholders identified in the first child workflow.


Outcome

Finish line

By now, you have successfully passed an array to a group node and processed each element inside a child workflow. Moreover, you have learned how to use the output of an inital group node, and pass this data to a second child workflow.