Salesforce Marketing Cloud AMPscript Basics
Salesforce Marketing Cloud AMPscript Basics: Variables, SET, and Printing Values
If you're starting with AMPscript in Salesforce Marketing Cloud (SFMC), the first concepts you need to understand are Variables, SET Statements, and Printing Values. These building blocks help you personalize email content using subscriber data.
1. What is a Variable in AMPscript?
A variable is used to store data temporarily while your email is being processed. In AMPscript, variables always start with the @ symbol.
Syntax
VAR @FirstName
Use Case
Suppose you want to store a subscriber's first name and use it later in the email.
2. Using SET to Assign Values
The SET statement assigns a value to a variable. The value can come from a Data Extension, system function, or static text.
Syntax
SET @FirstName = "Raj"
Complete Example
%%[
VAR @FirstName
SET @FirstName = "Raj"
]%%
Use Case
Store a customer's name in a variable so it can be reused multiple times within the email.
3. Printing a Variable Value
Once a value is stored in a variable, use the v() function to display it.
Syntax
%%=v(@FirstName)=%%
Complete Example
%%[
VAR @FirstName
SET @FirstName = "Raj"
]%%
Hello %%=v(@FirstName)=%%,
Resulting Outcome
Hello Raj,
4. Getting Values from a Data Extension
Most of the time, values are pulled directly from subscriber data instead of being hardcoded.
Code
%%[
VAR @FirstName
SET @FirstName = AttributeValue("FirstName")
]%%
Hello %%=v(@FirstName)=%%,
Use Case
Retrieve the subscriber's FirstName from a Data Extension and display it dynamically.
Resulting Outcome
| FirstName | Output |
|---|---|
| Raj | Hello Raj |
| Amit | Hello Amit |
| Blank | Hello |
5. Multiple Variables Example
Code
%%[
VAR @FirstName,@Country
SET @FirstName = AttributeValue("FirstName")
SET @Country = AttributeValue("Country")
]%%
Name: %%=v(@FirstName)=%% <br>
Country: %%=v(@Country)=%%
Resulting Outcome
Name: Rohan
Country: India
Complete Example
Code
%%[
VAR @FirstName,@Status
SET @FirstName = AttributeValue("FirstName")
SET @Status = AttributeValue("Status")
]%%
Hello %%=v(@FirstName)=%%,
Your account status is:
%%=v(@Status)=%%
Resulting Outcome
Hello Rohan,
Your account status is:
Active
Conclusion
Before learning IF statements, loops, or Data Extension lookups, it's important to understand these three AMPscript fundamentals:
VAR → Creates a variable.
SET → Assigns a value to a variable.
v() → Prints the variable value on the email.
These concepts form the foundation of AMPscript personalization and are used in almost every SFMC email template.
Comments
Post a Comment