Quickwork
Search…
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:
Figure 1: Foreach flowchart
Let’s assume a real-case scenario and practically understand the working of Foreach Loop.

Usecase

Consider a user 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 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:
Figure 2: Product data sheet of the company
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:
  1. 1.
    Create a new journey named Product details using Foreach.
  2. 2.
    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.
  3. 3.
    Click the Lik account button to connect a Facebook Page with Quickwork. Click here to know how to connect. The trigger looks like this:
Figure 3: Facebook Messenger trigger

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:
  1. 1.
    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.
  2. 2.
    Select the Contains condition from the drop-down in the Condition field.
  3. 3.
    Specify the Give me GMS datasheet keyword as the value in the Right Value field.
Figure 4: The IF Condition

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:
  1. 1.
    Click the Please choose and action bar and choose the Facebook Messenger app from the Apps drop-down menu and select the action, Send message, from the Actions list.
  2. 2.
    The same Facebook Page will get connected automatically as we connected first in the Event section.
  3. 3.
    In the Message type input field, select Response.
  4. 4.
    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.
  5. 5.
    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:
Figure 5: Facebook Messenger-Send message action under IF Condition

Configuring the Google Sheets action under If Condition

  1. 1.
    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.
  2. 2.
    For authorization, connect the Google account in which you had created the spreadsheet mentioned in the Prerequisite section.
  3. 3.
    Once a connection is created, the following fields will open up in which you need to enter the details.
  4. 4.
    Select the sheet, GMS Inventory Ltd. product dataset, from the Spreadsheet drop-down field.
  5. 5.
    In the Query String field, type Select *. This query operates on the entire sheet data respectively.
  6. 6.
    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.
  7. 7.
    Select Sheet 1 in the Sheet input field:
Figure 6: Google Sheets- Search cell using query action
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:
Figure 7: Foreach Loop

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:
  1. 1.
    Click on Please choose an action and choose the Facebook Messenger app from the drop-down menu in the App field.
  2. 2.
    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.
  3. 3.
    In the Message type input field, select Response.
  4. 4.
    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.
  5. 5.
    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
  6. 6.
    Drag and drop the above-mentioned data pills from the Foreach response under Data Tree Output:
Figure 8: Facebook Messenger under Foreach Loop
The complete journey flow looks like this:
Figure 9: The Product details using Foreach journey

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 Quickwork. You'll get the details of all the products available in GMS Inventory Ltd:
Figure 10: The journey execution
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 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 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.