Populating Dynamic Fields within your email with AMPscript

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.

IdEmailAddressFirstNameLastName
003f1234D8cuI01test01@gmail.comJohnDoe
003f1234D8cuI02test02@gmail.comSallySmith
003f1234D8cuI03test03@gmail.comGeorgeBaker
003f1234D8cuI04test04@gmail.comJaneDoe

Let’s suppose you want to populate the email with the first name as the greeting, for example:

Dear John,

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.

IdEmailAddressFirstNameLastName
003f1234D8cuI01test01@gmail.comJOHNDOE
003f1234D8cuI02test02@gmail.com
003f1234D8cuI03test03@gmail.comgeorgebaker
003f1234D8cuI04test04@gmail.comDoe

In each of these records, you can have improperly formatted values or null values. This would result in the greeting looking a bit weird:

Dear JOHN,
Dear ,
Dear george,
Dear ,

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)=%%,
Dear John,
Dear ,
Dear George,
Dear ,

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: test02@gmail.com and test04@gmail.com (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)=%%,
Dear John,
Dear Customer,
Dear George,
Dear Customer,

There you have it! A default value of “Customer” if the first name attribute is null by using the IIf function and properly formatting the first name value using ProperCase.

Leave a Reply

Your email address will not be published. Required fields are marked *