Ampscript : The basics
❓What is Ampscript?
It's marketing cloud's scripting language that enables us to write code which helps us with content personalization, dynamic content creation and data manipulation like insert, upsert, delete or fetch data from DE's. It's case insensitive.
❓How do we write an Ampscript function ?
1) %%=function=%%
Example
Hello, %%=Lowercase('ANANYA')=%%
Here 'lowercase' is the name of the function.
NOTE:- Inline code is just for interpreting and executing a single function. Whereas code block can be used for declaring, setting variables, making use of multiple functions and then outputting the code.
2) To let go of the above disadvantage we have something called as :- Code blocks
Syntax :
%%[
code
]%%
❓What are variables in Amspcript?
They are containers to store a value. They begin with a @ and we declare then using 'var'.
Example var @StoreValue
Use case:- If a function performs a + or a - operation then the result can be stored using a variable.
To use a variable always
Declare it - using var @
Set it - using Set @
Output it - using 'v' function for output
Example
%%[
var @num1, @num2, @num3
Set @num1= 100
Set @num2 = 200
Set @Num3 = Add( @num1, @num2)
output(v(@num3))
Note : We can only declare variable in an ampscript code block and not using inline ampscript.
❓What is the order of execution in ampscript?
Ampscript code is processed from top to bottom in the following sequence:-
HTML Body
Text Body
Subject line
Any variable declared in the subject line will be null in html body and text body so always declare variables in html body.