Foreach Condition

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

Foreach Condition is a loop 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 OTP manually to each attendee 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 2: Foreach journey

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 Event section, search for the HTTP app in the Apps list. 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 field will append to Quickwork’s static URL and a webhook URL will be generated. Refer to the following figure:

Figure 3: The HTTP trigger

The webhook name should be unique each time while configuring the HTTP app. Also, the hexadecimal user ID 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.

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 4: 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.

  6. Ignore Sample response. Select Sheet 1 in the Sheet input field:

Figure 5: Configuring the Google Sheets action

Using Foreach Condition

Click on the Add a step button (+ icon), click Business Logic, and select Foreach Condition. The Foreach Condition appears with a per-intended action bar. To give the appropriate input to Foreach Condition, click the Search cell using query action response under Data Tree Output and drag and drop the Rows data pill in the Source field:

Figure 6: 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 data pill from the Foreach output tree under Data Tree Output. Then append .Number to the data pill. The input is shown here:

Figure 7: Configuring the SMS by Gupshup action

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 strictly prohibits spamming. To send a custom message via text or formula, make sure the phone numbers are whitelisted 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 Save & Start 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 a 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.