for-sms-banner

For loops are a fundamental programming construct that allows you to repeat a set of actions a specified number of times. When integrated into workflows, for loops can make your processes smarter and more efficient by automating repetitive tasks. This blog post will guide you through the world of for loop conditions in workflows, demonstrating how to use them effectively.

How ForLoop Works:

A for loop is a control flow statement that executes a block of code for each item in a collection. This approach simplifies looping by allowing you to directly work with collections, making your workflows more intuitive and streamlined.

  1. Collection: The array or list of items that the workflow will iterate over.

  2. Current Item: The current item being processed in the loop.

By setting these values, you can control how many times the loop runs and tailor it to your specific needs.

1. Get Customer data from database

Create a Cosmos Read Many Block to fetch all customers from the database.

readmany-customers

2. Define the For Loop Collection:

  • Loop over collection: Set the customer collection.
for customer in customers:
// Send SMS to each customer

for-loop-items

3. Send SMS Using Twilio

Click on the expand icon on Unit Block and it will expand you to Builder page where you can build logics of the loop.

Sample Pseudo Code
for customer in customers:  
    twilioClient.messages.create({
        body: "Don't miss our special promotion!",
        from: '+YourTwilioNumber',
        to: customer.phoneNumber
    })

Add a Twilio block inside the loop. To access the current object in the collection, use the syntax below

{{$forLoop.val.phoneNumber}}

Make sure that Twilio is connected to the platform. Guide to Twilio integration can be found here

twilio-sms

4. Send Success Response

Create a Build JSON block to be used in Http Response block to send a successful response.

{
    "response": "Promotions sent successfully"
}

for-loop-response