1. Home
  2. Studio
  3. Advanced features

How to create a loop in your form

Sometimes it is necessary to repeat questions based on the amount given by your contact, for example, the name of each child or the car brand of each car. For this, you use loops. This manual explains how to create a loop in your form.

This manual is for advanced users. Please make sure you are comfortable working in script mode before continuing with this manual.

Use case 

For this example I want to know how many children my contact has, besides that I want to know for each child their name and birthdate. 

First, build the form with all the elements: 

studio

Next place {@position} in the key of each variable that needs to be repeated

By placing {@position} in the key of the element, you ensure that each element has a unique key. This is important because elements with the same key will have the same value.

@position

Go to script mode 

The next step will take place in script mode. In the top right corner click on Script

scriptmode

Add the Array to the script mode 

Next in script mode place the elements or the loop in an array. 

Write the following in script mode:

{

   “:array”: “{key of deciding element}”,

      “:fill”: [ 

           {data.key_1_{@position}, 

           {data.key_2_{@position}

               ]

}

Array

Make sure that you replace key of deciding element for the key of the deciding element. In this use case it is the deciding element is How many children do you have? with the key: data.how_many_children_do_you_have

You have now added a loop to your form

final_array