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:
Let’s assume a real-case scenario and practically understand the working of Foreach Condition.
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:
Action: Google Sheets and SMS by Gupshup
Loop: Foreach Condition
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:
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.
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:
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:
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:
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:
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.
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.
Once a connection is created, the following fields will open up in which you need to enter the details.
Select the sheet, Event Attendees, from the Spreadsheet drop-down field.
In the Query String field, type Select *. This query performs the operation on the entire sheet data respectively:
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:
Now, let us the SMS by Gupshup-Send message action in the intended action bar of Foreach Condition:
Click on Please choose an action and choose the SMS by Gupshup app from the drop-down menu in the App field.
Select the action, Send message, from the drop-down menu in the Action field.
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.
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:
SMS by Gupshup configuration looks like this:
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.