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
, andElseIf
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 Name | Description |
---|---|
MemberID | A unique member ID number for the subscriber |
FirstName | The subscriber’s first or given name |
LastName | The subscriber’s surname or family name |
PrefName | The subscriber’s preferred name |
Address | The subscriber’s street address |
City | The city that the subscriber lives in |
State | The state, province, or other subnational unit where the subscriber lives |
PostalCode | The subscriber’s postal code |
MemPref | The subscriber’s preferred outdoor activity |
Plat | A 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.
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.