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

Review AMPscript Basics

Learning Objectives

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

  • Review AMPscript language elements.
  • Identify AMPscript functions.
  • Use date and math functions.

Before You Begin

Before you begin, make sure you complete AMPscript for Marketers. While the badge is geared toward marketers with little coding experience, it serves as a great foundation for anyone learning AMPscript. The work you do here builds on the concepts and work you do in that badge. In this badge, we assume that you’re a technical marketer with basic knowledge of HTML and CSS. 

AMPscript Review

AMPscript is Marketing Cloud Engagement’s proprietary scripting language that helps marketers enhance their messages. From adding personalized content to retrieving relational data from data extensions, AMPscript is a powerful friend to have, especially as a technical marketer. 

In AMPscript for Marketers, you learn about the language of AMPscript from characters, variables, and operators. We even show practical examples of how to use strings of code. In this badge, we take you to the next level, where we help you start using AMPscript in specific use cases.

AMPscript Basics

Let’s get started by doing a quick review of AMPscript basics. The AMPscript language is procedural and processed top down. In other words, the system scans and outputs all AMPscript in the order it was written before sending the email to subscribers. Therefore, the system needs to know where the AMPscript code starts and stops. To identify AMPscript, the code must be surrounded by opening and closing delimiters or defined within a script tag. It’s a flexible language with three options for defining the AMPscript code. 

Type

Sample Code

Inline

%%=LowerCase(FavoriteAnimal)=%%

Block

%%[Output(LowerCase(FavoriteAnimal))]%%

Tag 

<script runat=server language=ampscript> Output(LowerCase(FavoriteAnimal))</script>
Note

In this module, we focus on inline and block AMPscript, but you can find examples of script tags on the AMPscript developer site.

Values

One of the core language elements of AMPscript are values, which can be either variables, constants, or attributes. Let’s start with variables. 

Variables 

A variable is a storage location for the information you want to reference later within a script statement or function. Variable names must begin with @ and include at least one other letter, number, or underscore. For example: @firstName or @first_Name.

Note

Spaces and commas aren’t allowed in a variable name.

Variables are declared in your code by using the keyword VAR followed by one or more comma-delimited variable names. To assign a value to a variable, use the keyword SET followed by the variable name, an equal sign, and the value. A variable’s value can be a constant or an attribute.

Constants and Attributes

Value Type

Description

Code Example

Constant

A constant is a value that can’t be altered by the system. These can be numeric, string, boolean, or null values. 

%%[ VAR @firstName
SET @firstName = "Susan"
]%%

Attribute

Attributes (or data extension values) refer to any content held inside a subscriber list or a data extension. They’re recognized as unquoted strings. Values that have a space or special character require opening and closing brackets. We recommend that you include opening and closing brackets around all values for consistency and troubleshooting.

%%[ VAR @firstName
SET @firstName = [First Name]
]%%

Data Comparison

A key component of AMPscript is the ability to compare and add data into an email. AMPscript uses common operators for data comparison criteria, like greater than, equal to, and so on. However, they also use a few additional operators.

Operator

Used For

==

Is Equal To 

!=

Not Equal To 

AND, OR, NOT

Comparison modifiers

Comparison operators are controlled in AMPscript by using parentheses. So if you’re looking for a new orange tabby cat or kitten, your comparison operators might be (Cat AND Orange) OR (Kitten AND Orange).

IF Syntax

Now let’s review the IF syntax, which is used to define data or narrow down results based on set criteria. 

Operator

Additional Info

IF 

To perform conditional processing.

ELSEIF

Multiple ELSEIF statements can appear within an IF block.

ELSE

Only one ELSE statement can appear within an IF block.

ENDIF

Use the ENDIF statement to end an IF block. Only one ENDIF statement must appear at the end of an IF block.

Example Code

Let’s see an example of how the IF syntax would be used to set a default first name for a customer whose first name is missing in your data.

%%[ VAR @firstName
SET @firstName = [First Name]
IF Empty(@firstName) THEN
SET @firstName = "NTO Rockstar"
ENDIF
]%%
Note

AMPscript isn’t case-sensitive, but it’s a good idea to pick a standard and be consistent in your code.

A Comment on Comments

Every good programmer knows how important it is to add comments to your code. Doing so makes code easier for yourself and others to understand the purpose of the code. But did you know that AMPscript comments don’t appear in the messages that your customers receive? This separates AMPscript comments from HTML comments—HTML comments can still be viewed after deployment by inspecting the source code of the sent message.

AMPscript Functions

The previous language elements are all used within functions. Functions are self-contained modules of code that accomplish specific tasks. Functions usually take in data, process it, and return a result. For example, the function %%=Format(@price, "C", 'en-US')=%% updates a product’s price to reflect the proper formatting for US currency.

Functions are written like this: FunctionName(X)

  • FunctionName: A unique name that describes the function.
  • ( ): Opening and closing parentheses.
  • X: The parameters being evaluated. When there are multiple parameters, use commas to separate them.

Functions can accept variables, constants, and attributes as parameters between the parentheses. 

Note

In the examples found in the rest of the module, functions are shown as FunctionName(1, 2, 3), which represent specific parameters.

Common AMPscript Functions

While AMPscript doesn’t support the creation of custom functions, there are plenty of built-in functions that you can add to your code. Let’s review some date and time functions and sample code. 

Date Functions

Click on the name of each function to see how to use it and example code with output. You can also click on the link for each function to view developer documentation.

Working with date and time values can take some practice. Here are some things to keep in mind.

  • There are many ways to format dates. See the FormatDate() function reference page for information and examples.
  • Marketing Cloud Engagement’s system time is Central Standard Time, and it doesn’t adjust for daylight saving time.

Math Functions

By learning how to do math with AMPscript, you can write complex math equations using data from data extensions, and using constants that you define. Let’s do some math. Click on the name of each function to see how to use it and example code with output.

Note

So many AMPscript functions to try! Here’s a full list of functions and descriptions to spark your ideas.

Now that you have a handle on the basic aspects of AMPscript, in the next unit we review AMPscript string functions.

Resources

Comparta sus comentarios sobre Trailhead en la Ayuda de Salesforce.

Nos encantaría conocer su experiencia con Trailhead. Ahora puede acceder al nuevo formulario de comentarios cuando quiera desde el sitio de la Ayuda de Salesforce.

Más información Continuar para compartir comentarios