One of the simplest ways of using AMPscript is populating your email with dynamic information such as the First Name of the recipient. In other email platforms, this is also known as a “merge field”
Suppose you had a simple sendable data extension below (“sendable” meaning the data extension you’re using to send to your audience):
More on Lists versus Data Extensions here.
Let’s suppose you want to populate the email with the first name as the greeting, for example:
The simplest way is just using the AMPscript inline code:
This would work perfectly in an ideal world if your data was clean. But let’s make a few more assumptions. Let’s pretend the data was not complete and not formatted properly.
In each of these records, you can have improperly formatted values or null values. This would result in the greeting looking a bit weird:
There are a couple of issues with these improperly formatted data:
- Proper case of the names, i.e. all uppercase “JOHN” or lowercase “george”
- Null values
Let’s address the first issue. Using the AMPscript function ProperCase, you can return the value with only the first letter in the word capitalized. But first let’s also set the variable for the first name field so that we can easily use it later. Simply written as:
%%[ set @FirstName = [FirstName] ]%%
Note that we have the AMPscript in a AMPscript block surrounded by opening and closing tags: %%[ ]%%
Now we can use ProperCase on that variable, which would result in a properly formatted first name. We also the v function to output the FirstName variable.
%%[ set @FirstName = ProperCase([FirstName]) ]%% Dear %%=v(@FirstName)=%%,
Now that we’ve addressed properly capitalizing the name, let’s focus on null values in the data. Remember that we had two email addresses with null values for their first name: email@example.com and firstname.lastname@example.org (refer to the table above).
In the example output above, it is still showing those null values incorrectly with a space after “Dear.” Let’s use a default value if there no First Name found. We’ll use a simplified If statement with the IIF function and check if the data is Empty.
%%[ set @FirstName = ProperCase([FirstName]) ]%% Dear %%=IIf(EMPTY(@firstname),”Customer”, @firstname)=%%,