AMPscript Variables

Variables are like name tags. You can identify what it is and then just reference the name. This make your AMPscript easy to follow, decipher and debug.

Contrary to popular opinion, variables are not required in AMPscript. You can use all of the functions, conditional statements and operators without using a single variable. That being said – it can quickly become more work than it is worth, not to mention error-prone. So I don’t recommend it. But don’t worry – Variables are easy to use.

Just think about it, once you know what a Banana is, any time anyone else mentions a Banana you know what they are talking about. But it is more than just knowing what people are talking about. It is also about keeping things short and succinct, so it is easy to follow. Imagine if every time you wanted to talk about someone you had to use their whole name? You’d get frustrated pretty quick. Take the following example – which of these reads easier:

Option A:
Mr Something-or-Other went over there. And then Mr. Something-or-other bought the yellow piece of fruit that has a peel that monkeys eat. After that Mr Something-or-other ate the yellow piece of fruit that has a peel that monkeys eat that now belonged to him.

Option B:
Mr Something-or-Other went over there. And then he bought a banana. After that he ate his banana.

Both tell the same story, but simply by replacing the description of the item with a name and using he/she/them pronouns the second one is shorter, clearer and makes more sense. By giving that value a name you can tell the code what item to get and what to do with it. Not to mention it makes it much easier to read.

Types of Variables

Variables are identified in AMPscript by the @ symbol. By default, when you declare a new variable in AMPscript it is empty field. One thing that makes AMPscript stand out is that you don’t need to declare what kind of data you are planning to store in each variable. Variables can be numberic, strings or boolean values. (You could change a variable from one type to another. I wouldn’t, as it would be easier to just have 2 variables, but you still could).

Data for the variables can be set as static fixed/constant values, or dynamic values with the data being pulled in. Dynamic data can be provided to the AMPscript either by being provided at the time of population, or can be pulled via lookups and retrieves.

If the data is being provided, for instance from the email’s audience data extension, it is known as an Attributes, and can be identified by the square brackets, for example [Attribute Name].

Lookup and Retrieve actions are done with AMPscript functions such as “LOOKUPROWS” & “RETREIVESALESFORCEOBJECTS”. These functions allow you to take a piece of data you have and use it to pull data out of other SFMC data extensions or synced Salesforce objects, respectively. (I will go over those in a future post, promise.)


Variable Names

Variable names’s can not have spaces. Instead try CamelCase or add a “_” or “-“. I use CamelCase for most of my variables. The few times that that doesn’t work or doesn’t read easily – then I use “_”. Regardless of which you select – pick one and stick to it.

Name the variable something easy to identify. (There are a few protected words that you can not use for variable names, but most everything is fair game.) Be careful with using short-hands for your names. I recommend that you NOT use super short short-hands. A good rule of thumb is that someone else should be able to look at your code and have a good idea what a variable is referencing.

Here is an example – Which of these is easiest to read?

  • @FirstName
  • @FName
  • @FN

While the second one is readable, the first one is still the easiest and anyone looking at the AMPscript would know exactly what was being stored on that variable. I have on occasion had very long variable names, so that I could easily identify what it was. An example of this would be if I had a @ParentFirstName and a @StudentFirstName. Again it is easy to tell them apart and know what was in each of the variables.


Trick of the Trade

Does what you are working on call for A LOT of variables? Try setting yourself up a simple spreadsheet to build out all the pieces before you even start. Here is a simple example that you can use to get you started:

https://docs.google.com/spreadsheets/d/1FaRG6pkgX_kTH1GFRJ_UdgU3M0MTq9TA9gwQxKVI_rg/edit?usp=sharing

I’ve put comments on each of the column headings with a quick explanation as to what goes where.

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.