Marketers

What is AMPScript? How to Personalize Your Marketing Cloud Emails

By Ralph de Mol

AMPscript is a powerful scripting language that you can use across emails, landing pages, SMS, and push notifications in Salesforce Marketing Cloud.

Not to be confused with Marketing Cloud Personalization (the product formerly known as Interaction Studio), AMPscript is the code you write to format data the way you prefer, to serve up to your audience.

Marketing Cloud Content Builder, with its easy-to-use dynamic content blocks, is a starting point. AMPscript comes in to reformat subscriber data, and it’s not as complex as you might think.

In this guide, I will show you 5 AMPscript tips I use regularly, that will make it easier for you to personalize your assets.

1. Use Propercase to Fix Casing

When different databases are connected, they sometimes save information in different formats. For example with all the letters upper- or lowercase like john or JOHN. This would cause names in your newsletter list to be capitalized incorrectly.

AMPscript has an easy fix for this. The Propercase() function will correctly format a name by capitalizing it properly. Propercase works by wrapping a piece of text or a variable:

Dear %%=propercase(‘ben’)=%%,

When previewing/sending your email, it should display:

“Dear Ben,”.

Instead of the static value “ben” you can use a field name from your data extension to make it dynamic:

%%=propercase(firstName)=%%

2. Create a Fallback Using AMPscript For Missing Values

But what if you can’t be certain that the first name variable in the data extension is filled? A great way to make sure that you aren’t getting blank spaces in your email is to replace the first name with a default value if the first name is empty within your data extension.

Of course, there are multiple ways of doing this; you could build custom content blocks to make sure this doesn’t happen, but creating two content blocks and a dynamic content block seems a bit excessive if all you want to do is replace one name.

A quick and easy way to do this is with an IIF statement (and no I didn’t misspell IF!). The extra I stands for inline and it looks like this:

%%=IIF(Not empty(firstName),firstName,’Customer’)=%%.

As you can see the IIF statement consists out of three parts.

This last part will be shown when the statement turns out to be false. In this example, we’ll use customer as our fall back but you could also use a lastname variable if you’d have it.

Not empty(firstName)The statement, this has to be either true or false. In this case, we are using “not empty()” but you can also use greater than, less than or equal statements.
firstNameThe text or variable that will be shown If the statement returns true or in our example, the firstName variable is not empty.
'Customer'This last part will be shown when the statement turns out to be false. In this example, we’ll use customer as our fall back but you could also use a lastname variable if you’d have it.

If we would write out the exact function of our script it would be:

If the firstName field in the data extension is not empty then show the firstName value, if it is empty show ‘Customer’.

This can be used for more than just filling in default values – you can use it for any dynamic content that has two possible outcomes.

3. Use AMPscript Replace to Clean Variables

This can happen when during an import, someone forgot to check the “Respect double quotes as text qualifier” checkbox. There are different ways of solving this; one is to re-do the import, but my preferred option is to use Replace(). Replace() allows you to replace one value with another, for example:

%%=Replace(firstName,'”‘,”)=%%

Let’s go through the separate parts of Replace().

firstNameIt always starts with the string or variable you want to replace parts of. In this case we are using a variable called gender (note the lack of single quotes).
' " 'This is the text Replace() will look for to replace with a different value. (I've  added extra spaces for legibility).
''This last part will be the value that will replace the previous text. Note that there is nothing between the single quotes. This will replace the quotes with nothing, essentially deleting the quotes.

You can use this to delete or replace any value. Other use cases involve replacing dots with commas (and vice versa), or adding underscores to values in links. Replace() is a very versatile piece of AMPscript!

4. Format Dates Using AMPscript

It might be difficult to use variable dates in your email. Dates from data extensions are formatted like this 2022-08-16 (yyyy-mm-dd) – which looks terrible. But, don’t worry, AMPscript has you covered.

With FormatDate() you can display dates in any way you like – it even has a neat translation feature. FormatDate() is a little more complicated than Propercase() but it does come with a built-in translator.

Let’s start with a complete DateFormat script:

%%=FormatDate(‘2022-08-16 09:00′,’ddddd dd MMMM yyyy’,’HH:MM’,’en-US’)=%%

2022-08-16 9:00The parameters within FormatDate start with the date you want to format, in this example I’ve used a fixed date but you can also use a variable. In that case, you don’t have to use the single quotes around it. Single quotes signify that the value is static.
ddddd dd MMMM yyyyThis part determines the way the date is shown, the time is determined separately. It will render as Thursday 3 october 2019. But you can format it anyway you like.
HH:MMTime can be displayed up to the mini second, or when you don’t want to display the time it can be left empty. However you can’t skip that parameter, so if you don’t want to display the time replace the,’HH:MM', with ,'',.
en-USThis designates the language and culture to display the date in. For example if we would’ve used nl-NL it would translate the day and month into Dutch, which has saved me a lot of time in the past.

This is a great way to display an order date on a receipt or today’s date by using NOW(), instead of an actual date if you want your newsletter to be more like a newspaper.

Note: Here’s a great cheat sheet for all your date formats.

5. Format Dates Different Timezones Using AMPscript

This can be a hassle. Due to SFMC servers using ‘Central Standard Time’, when it saves a default time to a data extension, your dates can be several time-zones off to what’s accurate and meaningful to your subscribers.

Luckily there’s an easy fix for this: SystemDateToLocalDate(). This will automatically set your date to the timezone of your Marketing Cloud instance (if the initial date is in the ‘CST’ timezone that is).

The use of SystemDateToLocalDate() is fairly easy, you just have to place the date within the script like so:

%%=SystemDateToLocalDate(‘2019-10-03 09:00’)=%%

Summary

These five scripts fix most of the formatting problems my marketer colleagues and clients have, and I hope they’ll fix your formatting problems as well. If not leave a comment and I’ll try to respond as soon as possible!

If this has whetted your appetite for dynamic content through AMPscript and you want to take your skills to the next level, I regularly host AMPscript workshops. In these workshops, we’ll cover everything from the basics to the more advanced topics like fully personalized emails. For more information check out: JourneyBuilder.eu.

READ MORE: Advanced AMPscript Use Cases for Salesforce Marketing Cloud

The Author

Ralph de Mol

Ralph is a Freelance Salesforce Marketing Cloud Consultant with lots of hands-on experience, which he uses to help companies build better email campaigns.

Comments:

    Shaun
    July 26, 2021 9:13 pm
    %%=IIF(Not empty(firstName),firstName,’Customer’)=%% This just replaces everything with customer even when first name is filled out. Probably need to look more into the proper code.
    Deepa
    February 19, 2022 4:51 pm
    Hi Shaun, The correct statement is %%=IIF(empty(firstName),firstName,'Customer')=%%. Hope this helps.
    uptoword
    November 11, 2022 12:19 am
    I'm glad to see that THE DRIP is continuing to provide great content on AMPScript. I'm a big fan of the tool and find it to be very helpful in my day-to-day work. Keep up the good

Leave a Reply