Foreach Condition

Using the Foreach Condition in Quickwork Journey Builder. Quickwork is a no-code, API-based SaaS platform with pre-integrated apps to automate business workflows.

Foreach Condition is a looping action that executes the indented actions until all their datasets (elements) are finished executing successfully. It iterates over the elements of the collection (an array of objects) and results in bulk execution of the tasks defined in the journey. For example, broadcasting messages on various channels to the customers for upcoming events. Foreach Condition obeys the following methodology:

Figure 1: Foreach flowchart

Let’s assume a real-case scenario and practically understand the working of Foreach Condition.

Usecase

Consider there’s an event for which tons of people have wished to join. When people gather at the event venue, each one would require a unique OTP so that they could self-register for the event easily. The OTP should be received on one go, else the event manager would go crazy sending each OTP manually which would eventually waste a lot of time.

To build this journey, let’s take the help of the following adaptors:

  • Trigger: HTTP

  • Action: Google Sheets and SMS by Gupshup

  • Loop: Foreach Condition

Figure 1: The Message broadcaster automation

Configuring HTTP trigger

Quickwork’s HTTP trigger lets you call or listen to the incoming data enough to shoot the rest of the journey. Let’s configure it with the help of the following steps:

  1. Under the Trigger section, search for the HTTP app and click on it. Then select the trigger event, New request on HTTP link, from the Triggers list.

  2. Enter a webhook name in the Webhook name field say, demo. The input of this filled will append to Quickwork’s static URL and a webhook URL will be generated. Refer to the following figure:

Figure 3: The webhook URL

The webhook name should be unique each time while configuring the HTTP app. Also, the hexadecimal user ID 5d64caa4ab254016bbf0726b is generated uniquely each time by the journey builder.

3. Select the GET method from the HTTP method field drop-down menu.

Hitting the given Webhook URL in the new tab of the browser starts executing the journey:

Figure 4: The HTTP trigger

Configuring Google Sheets action

Let’s have a Google Sheet that contains the data of the event attendees such as name and phone number. For better understanding, here’s the look of pre-created Google Sheet named Event Attendees:

Figure 5: Event Attendees Google Sheet

The sheet contains the details of 3 users as of now. As per your business requirement, the number of users could be more.

Now let’s configure the Google Sheets action. To do so, follow these steps:

  1. Under the Steps section, click the Simple Action option. Choose the Google Sheets app from the Apps drop-down menu and select the action, Search cell using query, from the Actions list.

  2. For authorization, select an existing account or create a new account to authorize and connect Google Sheets. If you are not logged in to your Google Sheets account, you will be asked to do so.

  3. Once a connection is created, the following fields will open up in which you need to enter the details.

  4. Select the sheet, Event Attendees, from the Spreadsheet drop-down field.

  5. In the Query String field, type Select *. This query performs the operation on the entire sheet data respectively:

Figure 6: Configuring Google Sheets action

Using Foreach Condition

Click on the + icon | the Business Logic option and select Foreach Condition. The Foreach Condition appears with a per-intended action bar. Now, click on the Foreach bar and provide input to the Source field. To give the appropriate input, click the Search cell using query output tree under Data Tree Output and drag and drop the Rows key. It is illustrated here:

Figure 7: Configuring Foreach Condition

Configuring SMS by Gupshup action

Now, let us the SMS by Gupshup-Send message action in the intended action bar of Foreach Condition:

  1. Click on Please choose an action and choose the SMS by Gupshup app from the drop-down menu in the App field.

  2. Select the action, Send message, from the drop-down menu in the Action field.

  3. For authorization, select an existing account or create a new account to authorize and connect SMS by Gupshup. If you are not logged in to your SMS by Gupshup account, you will be asked to do so. Click here to know how to connect SMS by Gupshup.

  4. Once a connection is created, the following fields will open up in which you need to enter the details: 1. In the Message field, switch to the Formula mode and enter the following OTP code:

    let OTP = Math.floor(Math.random() * 10000000);
    let message = "Your verification code OTP is a"+OTP +".";
    message;

    2. To specify the input in the Number field, switch to the Formula mode and drag and drop the Item key from the Foreach output tree under Data Tree Output. Then, append .Number to the key. The input is shown here:

Figure 8: Passing data key to the input field

The Number keyword appended is the column header of the Google Sheets Event Attendees we created. It contains the contact number of all the attendees.

SMS by Gupshup configuration looks like this:

Figure 9: SMS by Gupshup

SMS by Gupshup strictly prohibits spamming. To send a custom message via text or formula, make sure it is white-listed by Gupshup.

Executing the journey

Once your trigger and actions get successfully configured, click on the Save Changes button to save your journey. Then, click the Start Journey button to execute it.

Paste the HTTP Webhook URL in a new tab of your browser to ignite the journey. As soon as you hit the URL, Foreach Condition comes into play. It executes for each contact number independently in a loop, as per the query specified in Google Sheets, until all the contact numbers are finished receiving a unique OTP.

To see the complete execution, go to the History tab and click on the latest journey ID that just got executed. You will get a detailed history of journey execution highlighting parsed input/output of trigger and actions.

You’ll see that Foreach Condition has been executed thrice as there are three contacts in Google Sheets to which the SMS was to be sent. Each contact received a unique OTP over a single execution of the journey.