Foreach loop

This section explains the Foreach loop through a sample use case.

Foreach loop is a business logic that allows you to execute a set of actions for each item in a collection, such as records in a database or rows in a Google Sheet. It ensures that all actions are performed for each entry until all records are processed. This feature is ideal for tasks like broadcasting messages to customers for upcoming events, updating records in a CRM system, and much more.

The Foreach loop follows this method:

  1. Loops through each element in the dataset.
  2. Executes defined actions for each item in the collection.
  3. Continues until all elements are processed.

This loop enables bulk execution of tasks, making it efficient for handling large datasets dynamically.

Consider a user who wants to receive product details, availability, and costs from a company known as GMS Inventory Ltd. when a specific message is sent via Facebook Messenger. Given the dynamic nature of product details, the message must accurately reflect current information. This journey can be set up using the Foreach loop.

Prerequisites

  • Access to Quickwork.
  • Basic understanding of Quickwork terminologies and familiarity with Quickwork's GUI.
  • Experience in creating, executing, and managing a journey effectively from start to finish.
  • If you want to try this specific journey, you need, a Google Sheet named GMS Inventory Ltd. product datasheet that contains details classified under the columns Product, Units available, and Per unit cost. These columns serve as data pills in the journey.

Building the journey using the Foreach loop

The following steps describe how to use Foreach loop condition to share the details of multiple products listed in a Google Sheet, with a customer who requests the information via Facebook Messenger.

  • Trigger: A specific New text message received on Facebook Messenger.
  • Action: Send product details message by fetching information from Google Sheets using Search Cell Using Query by iterating using the Foreach loop if the message contains GMS datasheet.

Now, let us create the journey.

  1. Set up the Facebook Messenger trigger:

    1. Go to the Event section, select Facebook Messenger from the Apps list. Then select the trigger event, New text message, from the Trigger Event list.

    2. Connect your Facebook page by choosing an existing connection or creating a new one. To set up a new connection, click + Create new connection link and follow the prompts to link your Facebook page.

  2. Configure the If condition:

    1. Click on the Business Logic. Choose the If Condition.

    2. To set up the If condition block, drag and drop the Text Message data pill from the Facebook Messenger | New text message under Data Tree Output in the Left Value field.

    3. Select the Contains condition from the drop-down in the Condition field.

    4. Enter the specific message line Give me GMS datasheet in the Right Value field.

    5. Click the Please choose an action bar and choose the Facebook Messenger app from the App drop-down list.

    6. Select the action, Send message, from the Facebook Messenger Action list. The same Facebook page is connected automatically that you linked above in the Event section.

    7. From the Message type drop-down, select Response.

    8. In the Recipient field, drag and drop the Sender Id data pill from the Facebook Messenger | New text message response under Data Tree Output.

    9. In the Message text field, specify a sample message to be sent as a response to a user. For example: Here are the products' details of GMS Inventory Ltd:


  3. Configure the action:

    1. Click on the + icon and click the Simple Action option.
    2. Choose the Google Sheets app from the Apps drop-down list. Select the action, Search cell using query, from the Google Sheet Action list.
    3. Click Link an Account to connect the Google account which has Google Sheet mentioned in the Prerequisite section.
    4. Select the sheet, GMS Inventory Ltd. product dataset, from the Spreadsheet drop-down list.
    5. In the Query String input field, type Select *. This query operates on the entire sheet data respectively.
    6. Select the required worksheet from the sheet.
    7. In the Sample Response field, specify the column names of Google Sheets in JSON format, {"Product":"", "Units available": "", "Per unit cost":""}. The column names defined here act as data pills for other actions. Ensure that the column names are used as keys in the JSON object, where string values are enclosed in double quotes and numerical values should be entered without quotes. This formatting will ensure the data is accurately recognized and utilized as data pills in subsequent actions. Double-check the Hint text for format adherence and examples.
  1. Define the Foreach loop:
    1. Click on the + icon and then click Business Logic.
    2. Select Foreach Loop.
    3. To give the input to Foreach Loop, click the Google Sheets | Search cell using query action response under Data Tree Output and drag and drop the Rows data pill in the Source input field.
  1. Configure the action under Foreach Loop:

    1. Click on Please choose an action drop-down list and choose the Facebook Messenger app from the App list.
    2. Select Send message, from the Action drop-down. The same Facebook page that gets connected earlier will be automatically connected.
    3. From the Message type drop-down, select Response.
    4. In the Recipient field, drag and drop the Sender Id data pill from the Facebook Messenger | New text message response under Data Tree Output.
    5. In the Message text field, specify a sample message with associated data pills to be sent as a response to a user. For example, Product, Units available, Per unit cost. Drag and drop the above-mentioned data pills from the Foreach response under Data Tree Output.
  2. Click Save Changes.

Executing the journey

  1. Click the Save & Start button to execute the journey.
  2. Go to your Facebook Messenger and send the message Give me GMS datasheet to a Facebook page that you have connected with Quickwork. You will receive the details of all the products available in GMS Inventory Ltd.

Troubleshooting

  • Check connections:
    • Ensure that the Facebook Messenger and Google Sheets connections are active. If there are any issues with connectivity, try reconnecting or creating new connections.
    • Verify that the correct Facebook page and Google Sheet are linked to your journey.
  • Validate query and data pills:
    • Verify the query string in the Google Sheets action to ensure it accurately targets the intended column headers, identified as A, B, etc., within Google Sheets.
    • Ensure that the data pills dragged into the Foreach loop and message actions correspond to the correct columns in your Google sheet.

✏️ Tips and recommendations

  • Before running the entire journey, test each part incrementally. Start by testing the trigger, then the If condition, and finally the Foreach loop with its actions. This step-by-step approach helps isolate and fix issues more efficiently.
  • Ensure the column headers from the Google Sheet are accurately copied into the sample response in JSON format to avoid any additional spaces.
  • Utilize the Logger action in Quickwork to log intermediate outputs and errors. This is particularly useful for debugging complex journeys involving multiple actions and conditions.

📚 Additional resources

Journeys with business logic
If condition
If condition using AND/OR operator
If else condition
Try-catch error handling
Stop journey JSON format documentation
Understanding the Query String input field