AMPscript 201

This scenario contains information on these aspects of using AMPscript:

  • Using an if/else statement to insert a customer's preferred name
  • Inserting dynamic content into an email based on a customer's status
  • Inserting an estimated shipping date based on the customer's ZIP code

This AMPscript provides the content for the entire email message:

The Lookup() Function

This section of AMPscript sets the values of two variables (@city and @state) by retrieving information from the ShipTimes data extension:

The Lookup() function consults the ShipTimes data extension and looks for the city and state that correspond to the ZIP code value contained in the @zip variable. The function then assigns the applicable city or state to the variable.

Preferred Name If/Else Statement

The Preferred Name field involves two different if/else statements. The first statement determines whether or not the field displays in the email at all. If no preferred name exists, the message does not include the field. If the data extension includes a preferred name value, the email message includes both the field and the preferred name.

In this section of AMPscript, the Marketing Cloud application will insert a subscriber's preferred name from the sendable data extension when available. If the data extension does not include the value, then the application defaults to the customer's first name. The if statement looks for the preferred name and inserts it when available. If not, the else statement puts in the first name.

Screenshot of Members data extension

In the case of Christa Hirsch, the email send refers to the data extension shown above and inserts the preferred name of Chris into the email greeting.

Screenshot of email for Christa Hirsch

In the case of Shea Winslow, the email greeting defaults to the first name because of an unavailable preferred name value.

Screenshot of email for Shea Winslow

Platinum Status If/Else Statement

In the section of AMPscript shown below, the application determines whether the Platinum member status of the subscriber. If the subscriber did not join the Platinum program yet, the email contains a link prompting the subscriber to sign up on a separate web page. The email confirms Platinum status by displaying a capital Y.

The AMPscript [ if @plat == "N" then ]%% %%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) then displays the link if and only if the variable value equals N. Any other answer displays the value of the variable v(@plat) in the email.

In the screenshots previously displayed, the email sent to Shea Winslow shows the link because she is not a Platinum member. The email sent to Christa Hirsch displays a Y in that field because she is a Platinum member.