Get Started with AMPscript Development—Lesson 2

This lesson builds on Lesson 1, introducing you to more advanced AMPscript functionality.

  • Combining functions and variables
  • Dynamically changing the content of the message using If, Else, and ElseIf statements

Northern Trail Outfitters (NTO), the fictitious company that we refer to in this lesson, has a sendable data extension that contains several pieces of information about their subscribers.

Data Extension Column NameDescription
MemberIDA unique member ID number for the subscriber
FirstNameThe subscriber’s first or given name
LastNameThe subscriber’s surname or family name
PrefNameThe subscriber’s preferred name
AddressThe subscriber’s street address
CityThe city that the subscriber lives in
StateThe state, province, or other subnational unit where the subscriber lives
PostalCodeThe subscriber’s postal code
MemPrefThe subscriber’s preferred outdoor activity
PlatA true or false value that indicates whether the subscriber is a platinum rewards member

This code example contains the message content for this lesson.

This code resembles the example in Lesson 1, but includes some changes that further explore AMPscript functionality.

The AMPscript block at the beginning of the code example contains a new variable, @greetingName, with a value of Iif(Empty(@prefname),@fname,@prefname).

The Iif() function is a helpful tool for quickly evaluating a condition. When you use this function, you pass it a condition to test, followed by two values. If the condition is true, the function returns the first value. If the condition is false, the function returns the second value.

In this case, the condition that we’re testing is Empty(@fname). This condition calls another function, Empty(). The Empty() function returns true if the specified value is null or an empty string.

With one line of code, we called two functions and set the value of a variable based on the presence of subscriber data in a data extension.

Use If statements to define more complex conditional logic in your AMPscript code.

In our example, we use a basic If statement to test whether the subscriber has a preferred name that isn’t their first name. This condition uses the not operator and the Empty() function to test if the variable @prefname has a value—that is, the variable isn’t empty. If the subscriber has a preferred name, that value is included in the message. Otherwise, it’s omitted.

When you use an If statement, you must denote the end of the statement by including an EndIf statement.

The final section of AMPscript in the example performs another condition evaluation. Unlike the previous conditional evaluation, this one includes two tests. If the first condition is false, then the second condition is tested.

In this section, the AMPscript code checks the value of the @plat variable for each subscriber—that is, it checks to see if they’re a platinum tier member. If they aren’t, they’re shown a link that they can click to learn more about the benefits of being a platinum member. If they’re a platinum member, they see a confirmation of their status.

When you use AMPscript in your messages to perform conditional evaluation, it’s important to send test messages that cover as many conditions as possible. Choose multiple subscribers from your data extension and send yourself test messages that contain their data. Carefully compare the messages to make sure that the conditional evaluations were performed as expected.

This image shows how the email appears to a subscriber who has a preferred name, and who is a platinum rewards member.

An example email that includes the Preferred Name field. Next to Platinum Member, it says “Yes.”

A subscriber who doesn’t have a preferred name and who isn’t a platinum member sees a version of the email that resembles this example.

An example email that doesn’t include the Preferred Name field. Next to Platinum Member, there’s a link that says “See if you're eligible.”