Use Variables and Lookup Functionality
After completing this unit, you’ll be able to:
- Explain variables and how they are used in AMPscript.
- Identify when to use
Earlier in this module, we describe variables in AMPscript as a placeholder for data. Let’s review how this works by looking at how the code elements work together to define and set variables.
|Code||What It Does|
||Defines the placeholders.
||Define a value from the sendable data extension.
||Set a specific or literal value.
||Inline AMPscript used in your email content to return the value.
Let’s deconstruct these elements in an email that uses AMPscript.
1 . Start your code with
%%[ for the AMPscript block and add optional comments about the code using
2 . Declare the variables that you will be referencing in the message using the structure of
Var @createdname. You can use any variable name you want. Some people find it helpful to shorten a data extension field name or come
up with an easy to recognize, descriptive name. In this example, we use
Var @memid, @fname, @lname, and so on. Be sure to separate each variable with a comma.
3 . Next, assign the variables according to a sendable data extension’s field value. This is done by using
Set @name = ActualFieldName. When you set those variables, they need to match your declared variables from the second
step. Once done defining the fields you will use, end the block with a
Next is the fun part of actually writing the content!
4 . This block of code displays the data in the data extension to populate this piece of content. The code
%%= v(@fname) =%% is inline AMPscript that pulls in FirstName from your sendable data extension, which was identified
With a bit of CSS and formatting (and of course data in the corresponding data extension), this email populates like this using Preview and Test.
AMPscript is used extensively to pull data from data extensions using either
LookupRows(). These are helpful for when you need to grab information from data extensions that are not the one you are using for sending .
So for example, you might be sending a campaign to a segmented data extension of your top subscribers, but you need to reference your product data extension in your email to grab their most recent purchase.
LookupRows() functions allow you to—you guessed it—look for data in specified data extensions and return fields based on the values you include in the functions. Let’s review.
||Returns matching criteria for one data set on a table for one field of data.
||Returns matching criteria for one data set on a table, but returns data that is stored in several columns.
Sample Use Case
||Match loyalty level on subscriber’s record with loyalty table to display appropriate level in an email.
||Match booking reference number on subscriber’s table to bookings table to show flight number, flight time, seat number, boarding group.
Imagine you are baking a cake for your friend’s birthday. You wouldn’t make separate trips to the grocery store for flour, and then for sugar, and then for eggs. Similarly, it wouldn’t make sense to use Lookup() when you can get all the ingredients in one trip using LookupRows(). Using this function creates efficiency and helps with performance.
Ready to review what you’ve learned? The knowledge check below isn’t scored--it’s just an easy way to quiz yourself. To use it, drag the function in the left column to the matching purpose on the right. When you finish matching all the items, click Submit to check your work.
Great work! In the next unit we launch the Trailhead Simulator to practice using AMPscript in the application.