For loops in AMPscript
|NOTE: In this article, we will focus on understanding the basic concepts of working with loops in Salesforce Marketing Cloud. For a strictly technical specification, defer to the following article: AMPscript Process Loops.|
A for loop, which is actually the only loop available in AMPscript, lets you execute the same script repeatedly until an ending condition is met.
Let’s use the following example: we have a Data Extension with customer IDs and their ordered items. We want to use that dataset to send an email to each customer, listing the items each of them ordered.
Here’s our Data Extension called Orders:
|0031t000005D98UAAS||4632||Women’s Marine Hoodie|
|0031t000005D98UAAS||4632||Glass Love Bottle – 16.9 oz|
|0031t000005D98UAAS||4632||Eco Ballpoint Pen|
|0031t00000ZnTENAA3||3686||Men’s Marine Hoodie|
|0031t00000YEM4ZAAX||7354||Leather Hampton Watch|
In our example, the script contained within the loop would do the following: display a bullet point and display OrderItem name, and repeat the same action until all the OrderItems for a relevant Customer were displayed.
The most important element of any loop is the counter. The counter will define how many times the script in the loop should be executed. In order to define the counter, we must first set the criteria to find all the matching results in our Data Extension. We will use the LookupRows function to look up all rows, that match the Id of the Customer, that we want to send the email to. Then, we will use the RowCount function to determine how many results were found. The outcome of the
RowCount function will be our counter and will decide how many times the script inside the loop will be repeated. Here’s the first part of our script, where we count how many records match the criteria: [click here to see the code on Github]
Now let’s think through the loop logic. This is the verbal description of the loop that we would like to build:
1 and repeat the following in a loop until you reach the number defined in the
RowCount variable: display the current index and the name of the
OrderItem found in the referring row from the results of the
LookupRows function. Increase the index by one and repeat the process. Stop looping once the index reaches the number defined in the counter.
Here’s how this looks in AMPscript: [click here to see the code on Github]
You will notice the
next keyword at the end of the loop – each time this keyword is reached, the system compares the current index to the value of the counter defined by the
RowCount function. If the value is not equal to the end index, the loop will repeat until the end index is reached.
Here’s a full script that we can now use in an email, with added exception handling that will show “No items to display” in case the
LookupRows function doesn’t find any matching records for a subscriber in the Orders Data Extension: [click here to see the code on Github]
For a live coding example, see my YouTube video on Using AMPscript loops in Salesforce Marketing Cloud
We will create a similar solution to the one created in the AMPscript example above. We will use the same Data Exteniosn called Orders and we will use a loop to display all the OrderItems for a Customer on a CloudPage.
CustomerId into the script: [click here to see the code on Github]
The above script will return an ArrayList, which is a collection of elements. Arrays use numbers to access its elements and the numbering always starts at zero:
In AMPscript we used the
i) and the name of the element (
OrderItem) that has this index number in the ArrayList. Remember that we will start counting at “0”, not at “1” like we did in AMPscript.
Then, we will increase the index by one and repeat the process.
++ notation is the increment operator, which means that
i++ is exactly the same thing as
i = i+1. The script will stop looping once the index reaches the number defined in the counter (
rows.length). Here’s the script: [click here to see the code on Github]
Here’s the full script that we can now use on a CloudPage, with added exception handling that will show “No items to display” in case there are no matching records found in the Orders Data Extension: [click here to see the code on Github]
The is how the results will be displayed on a CloudPage:
- Computer Programming – Loops
- AMPscript Process Loops
- AMPscript 401 – Using for loops as part of an email send
Leave a comment below or email me at email@example.com.
Greatt read thankyou