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 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

Message Content

This code example contains the message content for this lesson.

%%[
  Var @memid, @fname, @lname, @prefname, @address, @zip, @mempref, @plat, @greetingName
  Set @memid = MemberID
  Set @fname = FirstName
  Set @lname = LastName
  Set @prefname = PrefName
  Set @address = Address
  Set @postcode = PostalCode
  Set @mempref = MemPref
  Set @plat = Plat
  Set @greetingName = Iif(Empty(@prefname),@fname,@prefname)
]%%

Hello %%= v(@greetingName) =%%! Here are your account details.

First Name: %%= v(@fname) =%%
Last Name: %%= v(@lname) =%%
%%[ if not Empty(@prefname) then ]
  %%Preferred Name: %%= v(@prefname) =%%
%%[ endif ]%%
Address: %%= v(@address) =%%
City:%%= v(@city) =%%
State: %%= v(@state) =%%
Postal Code:%%= v(@postcode) =%%
Shopping Preference: %%= v(@mempref) =%%
Platinum Member: %%[ If @plat == "False" then ]%%
%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%
  See if you're eligible
%%[ ElseIf @plat == "True" ]%%
  Yes
%%[ EndIf ]%%

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

Functions in Variable Assignments

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.

  Set @greetingName = Iif(Empty(@prefname),@fname,@prefname)

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.

Preferred Name If-Else Statement

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.

%%[ If not Empty(@prefname) then ]
  %%Preferred Name:%%= v(@prefname) =%%
%%[ EndIf ]%%

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

Platinum Status If-ElseIf 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.

Platinum Member: %%[ If @plat == "False" then ]%%
%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%
  See if you're eligible
%%[ ElseIf @plat == "True" ]%%
  Yes
%%[ EndIf ]%%

Test Multiple Variations

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.”