Getting started with AMPscript is as easy as 1,2,3. 1> Give it a name, 2> Give it a value & 3>Use the value.
Okay so lets break it down a little further.
STEP 1> Give it a name* — VAR
Declaring is what initiates the variable and adds it to the variable dictionary for that email or cloudpage. Each email and cloudpage operates its own variable dictionary, so you can use the same variable name in multiple places. We went over in the previous post, AMPscript Variables, some of the best practices for naming your variables. It is best practice to use the same variable name over and over for the same value. For example, if you are setting the First Name in multiple emails, use the same variable name. This keeps your data clean and your AMPscript blocks reuseable.
When it writes to the variable library, it sets up the variable with null or empty value. (It also will clear any value already on that variable, so make sure you declare your variables before you set your variables.)
Think of it like labeling an empty box. It doesn’t know yet what you are going to put into it, it only knows what you are going to call it.
Below is the syntax for declaring variables – you can declare one or many comma separated values.
SYNTAX – VAR
VAR @firstname, @secondname, @thirdname
%%[Var @FirstName, @LastName, @StreetAddress, @City, @State, @Zip ]%%
2> Give it a value — SET
The SET function is what is used to give a value to each variable name. There are two ways to think about this concept. We could continue with the box analogy – in which case, SET is how you color in your box. The other way to think about it is that you are providing the definitions of each variable, like you would do on vocabulary homework.
Either way – SET is one of the MOST used AMPscript functions.
SYNTAX – SET
SET @variablename = “value”
DEFINITION OF PARTS —
“VALUE” = Can be set to numbers, Strings (within Quotes), Boolean values (true/false) & Results of a Functions
%%[ Var @FirstName, @LastName, @StreetAddress, @City, @State, @Zip, @Age, @CollegeStudent, @Webpage, @CurrentDateTime, @PromoEnd Set @FirstName = “Sam” Set @LastName = “Johnson” Set @StreetAddress = “123 Main Street” Set @City = “Santa Clara” Set @State = “CA” Set @Zip = “95050” Set @Age = 22 Set @CollegeStudent = true Set @Webpage = “http://www.google.com” Set @CurrentDateTime = Now() Set @PromoEnd = '10/15/2018' ]%%
3> Use the variable —
Now that you have the variable declared and have set the variable it is time to use the variable.
Variables can be plugged into functions, added into inline requests or simply display the value.
When plugging it into a function within an AMPscript block you can just use @VariableName
To display it inline we use the variable function — v()
SYNTAX – V()
Using variables is just substitution. Lets say we have 3 variables –
%%[var @PersonName, @DegreeFocus, @UniversityName set @PersonName = "Ms. Jones" set @DegreeFocus = "Engineering" set @UniversityName = "Harvard"]%%
Lets convert the following sentence to display the variables we just set inline.
Original Sentence: Ms. Jones wants to study Engineering at Harvard.
Converted Sentence to use variables: %%=v(@PersonName)=%% wants to study %%=v(@Degreefocus)=%% at %%=v(@UniversityName)=%%
Let’s take the example of someone getting married. Oftentimes one partner will change their last name with marriage to match.
At the Beginning we have 2 people: %%[ var @HusbandFirstName, @HusbandLastName, @WifeFirstName, @WifeLastName set @HusbandFirstName = "Harry" set @HusbandLastName = "Smith" set @WifeFirstName = "Lexi" set @WifeLastName = "Brown" ]%% %%=v(@Husbandfirstname)=%% %%=v(@HusbandLastName)=%% and %%=v(@Wifefirstname)=%% %%=v(@WifeLastName)=%%. The wife's maiden name is %%=v(@Wifefirstname)=%% %%=v(@WifeLastName)=%%. After Marriage she changed her name to %%[ set @WifeLastName = @HusbandLastName]%% %%=v(@Wifefirstname)=%% %%=v(@WifeLastName)=%%.
This would print out:
At the Beginning we have 2 people: Harry Smith and Lexi Brown.
The wife’s maiden name is Lexi Brown.
After Marriage she changed her name to Lexi Smith.
Salesforce Documentation: AMPscript Language Elements
AMPscript Guide: Declare Variables, Set Variables, Using Variables
*AMPscript is a ‘loose’ language and does not require you declare a variable before you set it. However, it is best practice to declare it first. Please note – if you set it and THEN declare it, it will zero out the value. AMPscript nullifies the variable as part of the process of adding it to the variable dictionary for that email or cloudpage.