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> |
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.
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ย |
| 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 ]%%
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.ย
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.
Now that you have a handle on the basic aspects of AMPscript, in the next unit we review AMPscript string functions.
Resources
- Salesforce Developer: AMPscript Language Elements
- Trailhead: Marketing Cloud Engagement Programmatic Languages
- Trailhead: AMPscript for Marketers
