Skip to main content

Write AMPscript

Learning Objectives

After completing this unit, you’ll be able to:

  • Write a simple AMPscript statement.
  • Use AMPscript for specific use cases.

Write a Sentence

Now that you know the building blocks of AMPscript’s language, let’s discuss how you write it. We talked about math class before, so now let’s think back to language class. Remember when your teachers gave you writing assignments? Maybe they showed you how to write in different styles, such as poems, stories, and research papers. You learned that each style of writing is different, but all of them are correct. Similarly, AMPscript can be written in a few ways and all are correct. Let’s review these styles, a few examples, and when you might use them.  

Style Type
Sample Code
When to Use
Where to Use
Inline AMPscript
%%=LOWERCASE(FavoriteColor)=%%
Simple output
Free Form blocks
Code snippet blocks
HTML content blocks
AMPscript block
%%[Output(LOWERCASE(FavoriteColor))]%%
More complex code with multiple steps or conditional logic

Code snippet blocks
HTML content blocks
AMPscript tag
<script runat=server language=ampscript>
     Output(LOWERCASE(FavoriteColor))</script>

More complex code with multiple steps or conditional logic

Use if you are more familiar with web script syntax (JavaScript)
Code snippet blocks
HTML content blocks

The code examples all accomplish the same thing: A customer’s favorite color displays in lowercase in the sent email, even if the data is stored in all caps per the sample data in this chart.

Field Name
Data
FirstName
Susan
FavoriteColor
RED

Why do you need AMPscript for this, when you can just use personalization strings? Let’s review the difference below with email #1 using only personalization strings, and email #2 using both a personalization string and inline AMPscript. 

Email #1
Output
Dear %%FirstName%%,

We just received a new order of %%FavoriteColor%% jackets in your size.

Happy Shopping,
Customer Service
Dear Susan,

We just received a new order of RED jackets in your size.

Happy Shopping,
Customer Service
Email #2
Output
Dear %%FirstName%%,

We just received a new order of %%=LOWERCASE(FavoriteColor)=%% jackets in your size.

Happy Shopping,
Customer Service
Dear Susan,

We just received a new order of red jackets in your size.

Happy Shopping,
Customer Service

Code Examples

Now comes the good stuff: getting stuff done. Here are some common inline AMPscript code snippets to get you started with your new language.

Add Date

You can include simple requests like the date and time of the send in your message.

Code What It Does
Please respond to this email %%=Now()=%%.
Adds the send date to an email.

Currency Formatting Example

You can specify the format of a decimal field in a data extension to reflect currency.

Code What It Does
Amount Due: %%=Format(@price, "C", 'en-US')=%%
Updates a product’s price to reflect the proper formatting for US currency.

Data Cleaning

You can also manipulate text contained within a variable for your message.

Code What It Does
%%=ProperCase(FullName)=%%
Propercase: Returns specified string with the initial letter of each word capitalized.
%%=Lowercase(FavoriteColor)=%%
Lowercase: Returns the value in lowercase letters.
%%=Uppercase(FirstName)=%%
Uppercase: Returns the value in all uppercase letters.
%%=Format(Now(), "YYYY")=%%


Format: Returns the value according to the “string” you specify. This can be used to help manipulate data, for example, a year in a copyright footer.

Language Updates

You can change a greeting based on a customer’s preferred language. This function uses if, elseif, endif commands. Here is an example using an AMPscript tag. 

Code
What It Does
<script runat=server language=ampscript>
IF @language == 'FR' THEN
    SET @greeting = 'Bonjour!'
ELSEIF @language == 'SP' THEN
SET @greeting = '¡Hola!'
ELSE
    SET @greeting = 'Hi!'
ENDIF
</script>

  • If a customer’s language is identified as FR (French), the greeting displays as Bonjour!
  • If a customer’s language is identified as SP (Spanish), the greeting displays as ¡Hola!
  • If a customer’s language is set to anything else (or not provided), the greeting displays as Hi!

Even More Examples

Finally, here are some additional commands you can use.

Code What It Does
%%=Empty(@variable)=%%
Empty: Returns true if the value is an empty string or null. You can use this for a value that may be unknown or null, such as whether or not the subscriber has pets.
%%=Concat(@firstname," ",@lastname)=%%
Concat: Concatenates or links elements together. For example, you can use concat to display a full name, even if you stored First Name and Last Name separately in your data.
%%=RedirectTo(@samplelink)=%%
RedirectTo:  This function redirects a user to a specified URL.
/*Are you still reading this table?*/
Comments: Describe what your code is doing for others’ benefit, and for your own reference.

Using AMPscript in Emails

Now let’s get practical. Where do these handy new code examples go? Can you just paste them in anywhere in your email? Not quite. Let’s go back to our example that combines personalization strings and inline AMPscript to show how you can use AMPscript within Content Builder. Inline AMPscript can be added into most block types in Content Builder. 

Note

Need a refresher on Content Builder? Review the Trailhead module, Content Builder Features.

 Let’s walk through a quick how-to on creating a free-form content block. 

  1. Navigate to Content Builder.
  2. Click Create, then Content Blocks and then Free Form from the dropdown.
  3. Enter text, including any inline AMPscript.

Content Builder Personalization

Important to note: AMPscript blocks or tags can only be used in Code Snippet or HTML content blocks. Simply enter your AMPscript into the content block and click Done Editing.

AMPscript block code example in a Code Snippet block

In the next unit we take your learning a bit further, by using variables and lookup functionality to pull data into your emails from various data sources. 

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities