In the Template Builder, Loop is a step that iterates over all the objects of an incoming array of data. This way, you can perform a specific operation on all the objects of an array one by one.

In our templates, many steps fetch records from apps, usually in the form of a query. The response that is received is usually paginated and contains an array of multiple objects. Even if there is a single object, you will receive it in the form of an array. But you may need to import those records in another app that might require every item in the array separately to perform operations on it. This is where loops come in handy and help us in performing operations on every item of the array.

The loop step can be found in the list of available steps. 

Shown below is the loop step in a template in a collapsed state.

Once you add the loop step, you will need to provide an array for the loop to iterate over. This can be an array that you can directly provide here or you can provide an output from any step of the template that has your required data in an array. To provide the output of a step as the input array, you will give the array variable similar to {steps.2.steps.5.out}. This lets the loop navigate to the step where the output is your desired array.

You also have to provide the Variable Name. This is the name this step will assign to every object it iterates over. For example, if you have 10 objects in the array, you can provide the variable name as “Contact”. This will store the current object that the loop is iterating on in the “Contact” variable. For the first array object (first contact), the loop will store it in the “Contact” variable and move it to the next step which will update that contact in Salesforce. For the next iteration, it will store the next array object (second contact) in the new variable named “Contact” and move it to the update step, and so on.

 

As an example, let’s say that you have an integration of Salesforce contacts with Sendinblue app. If you create 3 contacts in Salesforce, a response from the “Contact Created in Salesforce” trigger will be received in your template. In its output, this trigger will provide the data for multiple contacts that are created at a time, thus sending the data in bulk. So the response from this trigger will be in the form of an array that will have 3 new contact objects and their attributes. This is the response generated from a trigger.

[{

"contact_v1": {

"id": "111",

"first_name": "Rose",

"last_name": "Gonzalez",

"email": "rose@example.com",

"customfields": “example_field"

}

},

{

"contact_v1": {

"id": "112",

"first_name": "Sean",

"last_name": "Forbes",

"email": "sean@example.com",

"customfields": “example_field"

}

},

{

"contact_v1": {

"id": "113",

"first_name": "Jack",

"last_name": "Rogers",

"email": "jrogers@example.com",

"customfields": “example_field"

}

}

]

Now for the integration to work, these contacts need to be created in the Sendinblue app as well. The contacts will be created with the “Create Contact” action for the Sendinblue app. However, our Salesforce Create Contact Action only works for one contact at a time. So you will need to provide all 3 new contacts separately for this action. For this, you can apply a loop that will iterate over all 3 objects of the array and perform the create step for every contact in Sendinblue. The simplified version of this example in a template, along with the loop body and the trigger step, is shown below.

In the above image, the step 0 is the trigger that gives the response output of the contacts created in Salesforce. Its output is given above. The loop step (step 1) is inserted after this. Putting the variable for the response trigger array as the input for the loop step and giving “NewContact” as a variable name will make the loop iterate over all the objects in the above array separately and store each object in a separate “NewContact” variable. The example of the first object in the iteration is shown as output below. This is the data for the first contact in the array. It will be passed on to all the child steps of the loop for the current iteration.

Output:

{

"contact_v1": {

"id": "111",

"first_name": "Rose",

"last_name": "Gonzalez",

"email": "rose@example.com",

"customfields": “example_field"

}

}

The data for the first contact will be sent to the child step of the loop (1.0). This is the action step that will create the current contact in Sendinblue.

With the above steps, the first iteration of the loop will be completed and the first contact of the array will be created in Sendinblue. The loop will run again for the second contact and so on. This way, all the contacts created in one app in bulk will be reflected one by one in the other app through the usage of the loop.

Just like updating contacts through loop, we can also use the loop step for deleting and creating contacts in a template.

Learn how to create your own templates here.

Did this answer your question?