Flow Builder - Utilising Loops

Working with FOR Loop Node

In many scenarios, you need to iterate over a collection of items and perform specific actions for each item. This is where FOR loop nodes become essential in Cosmocloud's flow builder.

Imagine you have a list of marks scored by a student and you want to calculate the total marks scored by the student. Here's how you can do it in Cosmocloud's flow builder:

Assumptions

  • You already have an API configured and a request body model configured with it which accepts marks as an array of floats.

  • To learn about how to set up APIs click here.

  • To learn about how to set up a request body model click here

Now, let's see how we can leverage the for loop node for iterating through the array of marks and getting the total marks of the student.

  1. Click Add Node, and select the Length of Array node. This will help us get the length of the array which will be useful for iterating through the loop. Configure this node with a variable name of len and pass in the array for which we need the length, as shown in the figure below.

  1. Now lets add a Set Variable node, this variable will be our iterator and help us fetch the marks at different positions in the array. Configure this node as shown in the figure below.

  1. Now let's add the For Loop node to iterate through the marks array. To configure this node we need to specify the variable we will be using to loop through the array, the condition when we want to get out of the for loop and the increment factor by which our variable should be incremented after each iteration. Configure the Variables to use field and the Increment factor field as shown in the figure below.

  1. Now let's add the condition, click on the Edit condition button and inside the JSON editor add the following condition.

5. Let's get inside the for loop click on the Get inside icon. You now are inside the for loop's flow and we will now create a flow here which we want to execute inside the for loop.

  1. Here we first use the Get Array Item node to get the item at a particular index, let's name it item.

  1. Now that we have the item, let's add it to our variable total using the `Add Variable` node.

  1. This is what our flow inside the for loop look like:

  1. Finally, let's return the total marks scored by the student by configuring the `HTTP Response` node as follows:

  1. This is what our entire flow look like:

  1. Let’s now hit this API with postman.

To learn more about the For Loop node click here

Last updated