Skip to main content

Use Variables and Lookup Functionality

Learning Objectives

After completing this unit, you’ll be able to:

  • Explain variables and how they are used in AMPscript.
  • Identify when to use Lookup() and LookupRows()functions.

Defining Variables

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
VAR @cats
Defines the placeholders.
SET @cats = catbreed 
Define a value from the sendable data extension.
SET @cats = "tabby"
Set a specific or literal value.
%%=v(@cats)=%%
Inline AMPscript used in your email content to return the value.

Let’s deconstruct these elements in an email that uses AMPscript.

Sample code with callouts based on sections of code.

1 . Start your code with %%[ for the AMPscript block and add optional comments about the code using /* and */.

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 a recognizable, descriptive name. In this example, we use VAR @memid, @fname, @lnameand 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 by SET @fname.

Note

If the data extension field name has a space in it, the field name needs to be contained in square brackets. Example: SET @fname = [First Name]

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. 

Email draft with photos and sample data using first name, last name, address, zip, member preference, and platinum status.

Pretty cool.

Note

Having issues with your code verifying? Be sure you are referencing the exact data extension field names, take a look at your commas, and confirm you closed the code block.  

Lookup Functions

AMPscript is used extensively to pull data from data extensions using either Lookup() and 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. Lookup()and 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.


Lookup()
LookupRows()
Description
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.
Code Sample
%%= Lookup("LoyaltyLevelDE","LoyaltyLevel","MemberID", @memId)=%%
%%= LookupRows("FlightInfoDE","Flight", Flight,"Seat",Seat Number, "Boarding", BoardingGroup) =%%
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.

Knowledge Check

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. 

Resources

Salesforce 도움말에서 Trailhead 피드백을 공유하세요.

Trailhead에 관한 여러분의 의견에 귀 기울이겠습니다. 이제 Salesforce 도움말 사이트에서 언제든지 새로운 피드백 양식을 작성할 수 있습니다.

자세히 알아보기 의견 공유하기