Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Explore AMPscript

Learning Objectives

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

  • Review the fundamentals and syntax for AMPscript.
  • Create inline AMPscript and AMPscript blocks.
  • Insert personalization strings.
  • Implement AMPscript functions and operators.
  • Work with AMPscript examples and best practices.

As a developer, you’re probably familiar with more than a few languages. And as a developer, you know that it’s impossible to learn everything about a language from a single document. This module introduces you to the basics of AMPscript, while also giving you the resources you need to dig deeper.

AMPscript Fundamentals and Syntax

At the most basic level, AMPscript calls act as a placeholder for the data requested from Marketing Cloud Engagement or a data extension. These calls can appear in one of three basic forms.

  • Inline AMPscript: %%=LOWERCASE(Name)=%%
  • AMPscript block: %%[ LOWERCASE(Name) ]%%
  • AMPscript tag:
<script runat=server language=ampscript>
   Lowercase(Name)
</script>

AMPscript can handle constant values, values from attributes and data extensions, and keywords. This example shows how to produce the same results for all three language elements.

  • 'example@salesforce.com' (note the quotes around the string constant)
  • SubscriberEmail (this string returns the value of SubscriberEmail from a data extension, which conveniently is example@salesforce.com)
  • @EmailAddress (this variable holds the value assigned to it earlier in the AMPscript block, which would be example@salesforce.com)
Note

Use your own email address or another valid address for testing.

AMPscript uses terms you’re probably familiar with, such as IF, ELSEIF, ELSE, and ENDIF. Take a look at this example to see how to implement these conditions in your AMPscript. 

<script runat=server language=ampscript>
   IF @region == '1' THEN
      SET @greeting = 'Bonjour!'
   ELSEIF @region == '2' THEN
      SET @greeting = 'Hola!'
   ELSE
      SET @greeting = 'Hi!'
   ENDIF
</script>

You can include as many ELSEIF statements as necessary for all the conditions you need to evaluate.

You can also set up FOR loops in AMPscript to iterate over content as many times as necessary. This example sets the applicable first name values for the number of included roles.

%%[FOR @Position = '1' TO @Position = @rowCount DO ]%%
   SET @FirstName = FirstName
%%[NEXT @Position]%%

And, of course, you can assign variables (declared as VAR) in AMPscript using SET.

%%[ VAR @text
    SET @text = "Hello, world!"
    Output(v(@text))]%%

Notice how the Output() function prints the value of the variable contained in the v().

For email messages, remember that AMPscript processes the HTML body of the message first (including any preheader), then the text body. The subject line goes last.

AMPscript Functions

AMPscript uses functions—an extremely large number of functions. We divide functions into several different categories.

AMPscript Function
What It Does
API
Create SOAP API interactions
Contacts
Modify Marketing Cloud Engagement contact information
Content
Modify Marketing Cloud Engagement content, such as text and images in email messages.
Data Extension
Modify data in data extensions
Date Time
Modify date and time information in Marketing Cloud Engagement
Encryption
Encrypt and decrypt Marketing Cloud Engagement data
HTTP
Get, post, and modify HTTP information in Marketing Cloud Engagement
Math
Perform basic math functions
Microsoft Dynamics CRM
Interact with Microsoft Dynamics CRM data
Salesforce
Interact with Sales Cloud data in Marketing Cloud Engagement
Sites
Interact with CloudPages sites
Social
Interact with Social Forward functionality in Email Studio
String
Modify string information in Marketing Cloud Engagement
Utilities
Return and evaluate types of Marketing Cloud Engagement data

Personalization Strings

AMPscript offers some easy strings to pull information into content, email addresses, and other areas where you need to dynamically assign values. These personalization strings return data from Email Studio lists and from the sendable data extension used for a send. Use AMPscript functions to return content from data extensions in other Marketing Cloud Engagement functions.

Impression Tracking

AMPscript lets you determine which sections of an email message perform better in sends. We may know intuitively that random cat pictures attract more attention than a block of text, but now you have the statistics to prove it! Surround a piece of content—pulled in using the ContentArea() or ContentAreaByName() functions—with the BeginImpressionRegion() and EndImpressionRegion() functions, and use impression tracking reports to see how your cat pictures measure up.

So What Do I Do with AMPscript?

You can perform a number of tasks in Marketing Cloud Engagement using these functions. That said, AMPscript is usually used to pull data from data extensions using calls like Lookup() and LookupRow(). These functions look for data in specified data extensions and return fields based on the values you include in the functions. This example looks up purchases made by a contact identified by a member ID and returns any rows containing purchase information.

%%[ VAR @row
    SET @row = LookupRows("Purchases","MemberID",@memID)]%%

This example shows how to retrieve a content area from Content Builder in an email message, using the key value.

%%=ContentBlockbyKey("myContentBlock")=%%

Once you retrieve that information, you can perform all manner of modifications and transformations to accomplish what you need.

Next, let’s take a look at Server-side JavaScript (SSJS).

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback