Foreach Loop
Loop all the records present in a sheet/database and perform the operations until all the records are finished parsing. Foreach Loop performs all actions in a workflow for each record or entry.
Foreach Loop 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 a journey. For example, broadcasting messages on various channels to the customers for upcoming events. Foreach Loop obeys the following methodology:
Usecase
Consider a user who wants to get the details of products, their availability, and the cost of a specific company, say GMS Inventory Ltd. The user must get these details when a specific message line is sent via messenger. As the product details are dynamic, the message sent to a user must be harmonized depicting accurate product details of the company.
To build this journey, let’s take the help of the following apps:
- Trigger: Facebook Messenger-New text message
- Action: Facebook Messenger-Send message and Google Sheets-Search cell using the query
- Condition: If Condition and Foreach Loop
Prerequisites
Let’s create a Google Sheet that contains the product details of GMS Inventory Ltd. Name the sheet as GMS Inventory Ltd. product datasheet:
The sheet contains the dummy details classified under the columns namely Product, Units available, and Per unit cost. These columns serve as data pills in the journey. More columns and details could be introduced as per your requirements.
Configuring the trigger
Let’s configure it with the help of the following steps:
- Create a new journey named Product Details using Foreach.
- Under the Event section, search for the Facebook Messenger app in the Apps list. Then select the trigger event, New text message, from the Triggers list.
- Click the Lik account button to connect a Facebook Page with the Gupshup integration platform. Click here to learn how to connect. The trigger looks like this:
Configuring the If Condition
Configure an IF Condition for a specific message line. This step is crucial because the user can send numerous messages daily. The journey must send the product details only when the message line is entered from the user end. The message line is Give me GMS datasheet.
To configure the IF Condition, click the Business Logic option and select If Condition. You will get the following fields in the Setup If condition block:
- In the Left Value field, drag and drop the Text Message data pill from the Facebook
Messenger | New text message Trigger under Data Tree Output. - Select the Contains condition from the drop-down in the Condition field.
- Specify the Give Me GMS datasheet keyword as the value in the Right Value field.
Configuring the Facebook Messenger action
Now let’s configure the Facebook Messenger-Send message action. This action sends an introduction message to a user as a response:
- Click the Please Choose an action bar and choose the Facebook Messenger app from the Apps
drop-down menu and select the action, Send a message, from the Actions list. - The same Facebook Page will get connected automatically as we connected first in the Event
section. - In the Message type input field, select Response.
- In the Recipient input field, drag and drop the Sender Id data pill from the Facebook Messenger |
New text message Trigger response under Data Tree Output. - In the Message text input field, specify a sample message to be sent as a response to a user. E.g.,
Here are the products' details of GMS Inventory Ltd:
Configuring the Google Sheets action under the If Condition
- Click on the Add a Step button (+ icon) and 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, connect the Google account in which you had created the spreadsheet
mentioned in the Prerequisite section. - Once a connection is created, the following fields will open up in which you need to enter the
details. - Select the sheet, GMS Inventory Ltd. product dataset, from the Spreadsheet drop-down field.
- In the Query String field, type Select *. This query operates on the entire sheet data respectively.
- In the Sample Response field, specify the column names of Google Sheet in JSON format. E.g., {"Product":"", "Units available": "", "Per unit cost":""}. The column names defined here act as data pills for other actions.
- Select Sheet 1 in the Sheet input field:
Now drag and drop this action under the previous action configured which is Facebook Messenger-Send message.
Using Foreach Loop
Click on the Add a step button (+ icon), click Business Logic, and select Foreach Loop. The Foreach Loop appears with a per-intended action bar. To give the appropriate input to Foreach Loop, click the Search cell using query action response under Data Tree Output and drag and drop the Rows data pill in the Source field:
Configuring the Facebook Messenger action under Foreach Loop
Now, let us the Facebook Messenger-Send message action in the intended action bar of Foreach Loop. This action sends the product details of the company:
- Click on Please choose an action and choose the Facebook Messenger app from the drop-down menu in the App field.
- Select the action, Send message, from the drop-down menu in the Action field. The same Facebook Page will get connected automatically as we connected first in the Event section.
- In the Message type input field, select Response.
- In the Recipient input field, drag and drop the Sender Id data pill from the Facebook Messenger | New text message Trigger response under Data Tree Output.
- In the Message text input field, specify a sample message with associate data pills to be sent as a response to a user. For example,
Product: Product
Units available: Units available
. Per unit cost (INR): Per unit cost - Drag and drop the above-mentioned data pills from the Foreach response under Data Tree Output:
The complete journey flow looks like this:
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.
Go to your Facebook Messenger and send the message with a message line Give me GMS datasheet to a Facebook Page that you have connected with the Gupshup integration platform. You'll get the details of all the products available in GMS Inventory Ltd:
To see the execution details, go to the History tab and click on the latest journey ID that just got executed. You will get the transaction report of journey execution highlighting the parsed input/output of a trigger and actions.
You’ll see that Foreach Loop has been executed four times as there are four products in Google Sheets for which the message is sent. If there are more products in the sheet, Foreach Loop iterates over all of those and sends them as an individual entity.
Updated over 1 year ago