Get Started with AMPscript Development—Lesson 1

This lesson introduces you to the basics of using AMPscript in your messages.

  • Creating variables
  • Using variables to refer to values in a data extension
  • Displaying the value of a variable in a message

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.

If you’re new to AMPscript, it’s helpful to move through the AMPscript sample code line by line so that you can better understand how AMPscript works.

The example begins with %%[, which indicates the beginning of a block of AMPscript code.

The second line of the example begins with Var, followed by several names that begin with @. Each of these names is used as a variable later in the code example. The @ sign is required at the beginning of each AMPscript variable. At this point, all of the variables have been declared, but the value for each one is null.

The next several lines begin with Set, followed by the name of one of the declared variables. These lines assign a value to each of the declared variables. The value of each variable is set to equal the corresponding column in the sendable data extension that this email targets. When the email is sent, the system retrieves the value in the specified column for each subscriber and assigns that value to the variable.

After the variable assignments is the string ]%%. This string indicates the end of a block of AMPscript code.

After the initial AMPscript block is closed, we see the first line of the message body. This line is the first that produces content that’s visible to the subscriber.

This line contains the text %%= v(@fname) =%%. The %%= indicates the beginning of a string of inline AMPscript code, and the =%% indicates the end. Between these tags is the function v(@fname). The v() function is a utility function that outputs the value of a variable. The function refers to the @fname variable.

When a subscriber receives an email that contains this code, their name appears in the text instead of the AMPscript code.

If you’re new to Marketing Cloud Engagement, see Get Started with Email Studio to learn more about creating an email.

This lesson assumes that you’re familiar with creating an email and that you already set up Marketing Cloud Engagement to send emails.

  1. In the Email Studio editor, paste the sample AMPscript into a content area.

A sample message in the Email Studio content editor. The sample AMPscript code from this lesson is pasted into a content area in the message.

  1. When you finish editing the message content, click Next.
  2. On the Preview and Test page, click the Subscriber Preview tab.
  3. Click the folder icon.
  4. Expand the Data Extensions folder, and then select a data extension that contains the data for your target subscribers.
  5. Select a subscriber. When you send the email, that subscriber’s information is populated in the message body.

The Subscriber Preview tab on the Preview and Send page. A folder structure on the left side shows an expanded folder labeled Data Extensions. Under that folder, a data extension called NTO Customer List is selected. A list of subscribers is shown on the right side of the page, and the third row of data is selected.

  1. On the Test Send tab, choose whether to send the test message to Individuals or to a Test data extension. If you choose to send to individuals, enter their email addresses. If you choose to send to a data extension, choose a testable data extension.
  2. Click Send Test.
  3. Review the test message. In the test message, the inline AMPscript code in the message body is replaced with subscriber data from the row you selected in the data extension.

An image of the test email. The fields in the message, such as First Name and Last Name, are populated with values from the selected data extension.