Get Started with AMPscript Development—Lesson 4

This lesson expands on Lesson 3, exploring advanced AMPscript topics.

  • Using For loops
  • Dynamically changing the appearance of a message using AMPscript
  • Filtering message context

In addition to the sendable data extension and ShipTime data extension from the previous lessons, this example refers to a data extension that contains information about a subscriber’s purchase history.

This code example contains the message content for this lesson.

The first part of this code example is similar to the example in lesson 3. However, the second part inserts information about the subscriber’s previous purchases and total amount spent into the email.

First, the code creates a set of variables to use in inserting product and price information into the email.

Next, the code assigns values to the @totalPurchases and @indicator variables. The @totalPurchases variable stores the amount of the subscriber’s total purchases. The @indicator variable alternates the background color for each purchase.

The code consults the Purchases data extension to find all of the subscriber’s previous purchases based on their member ID.

The @rows variable is set to the resulting rowset.

If there aren’t any rows in the rowset, it means that there isn’t any purchase data to display. We want to be sure that the rowset contains at least one row, so we use the RowCount() function.

Next, the code uses a For loop to step through the rowset that we obtained from the data extension. The For loop repeats an equal number of times to the number of rows in the rowset.

The row() function obtains the data in all of the columns in the current row.

In each row, we use the Field() function to set the values of the product variables. We also use the Add() function to keep a running total of the amount of money the subscriber spent on their purchases.

The cod sets the background color for the current row to white or gray depending on the background color of the previous entry. Odd-numbered entries have a white background, and even entries have a gray background.

The code uses the ContentBlockByName() function to retrieve an existing content block. The purchase data is formatted in the same manner as the content block.

The code then instructs the For loop to proceed to the next row.

The final part of the code makes sure that the numerical value in the @totalPurchases variable contains exactly two decimal places. It starts by determining the current number of decimal places by using the IndexOf(), Length(), and Subtract() functions.

  • The IndexOf(@totalPurchases,".") function returns the position in the value of the @totalPurchases variable where a decimal point (.) character occurs. If the variable doesn’t contain a decimal point, the function returns 0.
  • The Length(@totalPurchases) function returns the number of characters in the value of the @totalPurchases variable.
  • The Subtract(...) function returns the result of subtracting the value returned by the IndexOf(...) function from the value returned by the Length(...) function.

If the resulting value of this computation is 1, it means that the value of the @totalPurchases contains a decimal point followed by one digit. Because we want to show the subscriber a currency value with two decimal places, we use the Concat() function to add a zero to the end of the value.

If the value of the @decimalChar variable equals the number of characters in the value of the @totalPurchases variable, it means that the @totalPurchases didn’t contain a decimal point. In this case, we can use the Concat() function to add a decimal point followed by two zeros.