What is AMPscript?

AMPscript is a Salesforce proprietary scripting language that interacts with Marketing Cloud emails, landing pages, SMS and MMS messages and push notifications. By learning the AMPscript language, you can do any of the following:

Can add personalized, unique content for each subscriber.
• Example Subject Line: Hello, First Name!

Display content based on certain conditions.
• Example Subject Line #1: Hello, First Name!
• Example Subject Line #2: Hello, Valued Customer!

Format Date Values
• Example Date: Date might provide a date as “MM/DD/YYYY” but we want it to show as “Month Day, Year” instead.


AMPscript Syntax

As mentioned, AMPscript is a scripting language, and when you learn any new language, you first have to master how it works. AMPscript uses the following langauge elements.

• Constants
• Attributes and Data Extension Values
• Variables (or Keywords)


AMPscript Constants

Numeric Values
Numeric constant values consist of one or more digits. They are formatted as unquoted numerals. They can include 1 decimal point and an introductory minus sign for negative numbers. Commas, are not permitted.

<!-- Example Code -->
%%[
/**
* Constant (Numeric Values)
* Numeric constant values consist of one or more digits. They are formatted as unquoted numerals. They can include 1 decimal point and an introductory minus sign for negative numbers. Commas, are not permitted.
*/
VAR @number1, @number2, @number3
SET @number1 = 123
SET @number2 = -123
SET @number3 = 123.456
]%%
%%=v(@number1)=%%<br>
%%=v(@number2)=%%<br>
%%=v(@number3)=%%
<!-- Example Output -->
123
-123
123.456
View this gist on GitHub

String Values
A string is a set of one or more letters, numbers, spaces or special characters. These constant values must be surrounded (delimited) by double or single quotes. They can also include the same type of delimiting quote inside by doubling it.

<!-- Example Code -->
%%[
/**
* Constant (String Values)
* A string is a set of one or more letters, numbers, spaces or special characters. These constant values must be surrounded (delimited) by double or single quotes. They can also include the same type of delimiting quote inside by doubling it.
*/
VAR @string1, @string2, @string3, @string4
SET @string1 = 'String text goes here'
SET @string2 = "String text goes here"
SET @string3 = "My string's inside quotes!"
SET @string4 = 'My string''s inside quotes!'
]%%
%%=v(@string1)=%%<br>
%%=v(@string2)=%%<br>
%%=v(@string3)=%%<br>
%%=v(@string4)=%%
<!-- Example Output -->
String text goes here
String text goes here
My string's inside quotes!
My string's inside quotes!
View this gist on GitHub

Boolean Values
Boolean constant values must use the words true or false. They are not surrounded by quotes. They are also NOT case sensitive.

<!-- Example Code -->
%%[
/**
* Constant (Boolean Values)
* Boolean constant values must use the words true or false. They are not surrounded by quotes. They are also NOT case sensitive.
*/
VAR @toggle1, @toggle2, @toggle3, @toggle4, @toggle5, @toggle6, @toggle7, @toggle8
SET @toggle1 = true
SET @toggle2 = false
SET @toggle3 = True
SET @toggle4 = False
SET @toggle5 = TRUE
SET @toggle6 = FALSE
SET @toggle7 = 1
SET @toggle8 = 0
]%%
%%=v(@toggle1)=%%<br>
%%=v(@toggle2)=%%<br>
%%=v(@toggle3)=%%<br>
%%=v(@toggle4)=%%<br>
%%=v(@toggle5)=%%<br>
%%=v(@toggle6)=%%<br>
%%=v(@toggle7)=%%<br>
%%=v(@toggle8)=%%<br>
<!-- Example Output -->
True
False
True
False
True
False
1
0
View this gist on GitHub

NULL Values
A NULL value represents the absence of a value. The use of a NULL value is just a pointer, which is pointing to nowhere in memory.

<!-- Example Code -->
%%[
/**
* Constant (Null Values)
* etaphysical.
*/
VAR @string1, @string2, @string3, @string4
SET @string1 = "String1 Copy Here"
SET @string2 = ""
SET @string3 = ''
SET @string4 = 'String4 Copy Here'
]%%
%%=v(@string1)=%%<br>
%%=v(@string2)=%%<br>
%%=v(@string3)=%%<br>
%%=v(@string4)=%%
<!-- Example Output -->
String1 Copy Here
String4 Copy Here
View this gist on GitHub

Attributes & Data Extension Values

Attributes and data extension values refer to any content held inside a subscriber list or a data extension. If the name includes a space or special character, you must surround it with opening and closing brackets. Best practice to always include the opening and closing brackets when using attributes. This adds consistency when troubleshooting.

Example Use Case
Let’s say you’re building an email and the data extension you’re sending to has two fields called “Birthdate” and “FirstName”. The example below demonstrates how you can pull the values from those fields to use within your email.

<!-- Example Code -->
%%[
/**
* Attributes & Data Extension Values
* Below is an example how you can use attributes to personalize your email.
*/
VAR @Birthdate, @FirstName
SET @Birthdate = [Birthdate]
SET @FirstName = [FirstName]
]%%
<p>Hi %%=v(@FirstName)=%%, your birthdate is %%=v(@Birthdate)=%%.</p>
<!-- Example Output -->
Hi John, your birthdate is 12/12/1985.
View this gist on GitHub

Variables (or Keywords)

A variable is a storage location for information you want to reference later on. A variable includes two parts, an identifier and a value. The Identifier is a symbolic name you want to use. The Value is the information you want to store.

Declaring Variables
When you declare a variable, you are telling the system that the variable exists. By default, whenever you declare a variable that variable will be set to NULL.
• Example: VAR @FullName

Setting Variables
When you’re ready to set a variable you can assign values to it.
• Example: SET @FullName = “John Doe”

<!-- Example Code -->
%%[
/**
* Declaring & Setting Variables
* Below is an example how to declare and set variables with AMPscript.
*/
VAR @FullName
SET @FullName = "John Doe"
]%%
<p>Hi %%=v(@FullName)=%%, your shipment has arrived!</p>
<!-- Example Output -->
Hi John Doe, your shipment has arrived!
View this gist on GitHub

Gallery