AMPscript Guide: With Simple Examples You Can Use Today

If you work with Salesforce Marketing Cloud for more than five minutes, you’ll eventually run into AMPscript. It’s everywhere — powering personalisation, driving dynamic content, pulling data from Data Extensions, and quietly making your emails feel smarter than they actually are.

But here’s the thing: AMPscript looks intimidating until someone explains it properly. Once you understand the basics, it becomes one of the most useful tools in your Marketing Cloud toolkit.

This guide is designed to give beginners a clear, practical introduction to AMPscript — what it is, how it works, and how to start using it confidently. No jargon. No over‑engineering. Just the essentials, explained simply, with examples you can copy and paste straight into your emails or landing pages.

What Is AMPscript?

AMPscript is Salesforce Marketing Cloud’s scripting language. Think of it as the glue that connects your content to your data.

It lets you:

  • Personalise content at scale
  • Pull data from Data Extensions
  • Build dynamic logic
  • Manipulate strings, dates, and numbers
  • Trigger conditional content
  • Work with API functions
  • And generally make your emails behave intelligently

AMPscript runs server‑side, meaning the subscriber never sees the code — only the output. That’s why it’s so powerful: you can build logic that adapts to each individual subscriber without exposing any of the underlying mechanics.


Where Can You Use AMPscript?

AMPscript works in several places across Marketing Cloud:

  • Email Studio (subject lines, preheaders, body content, dynamic blocks)
  • CloudPages (landing pages, microsites, forms)
  • SMS messages (limited but still useful)
  • Journey Builder (via activities like Update Contact or SMS sends)

If you’re just starting out, email is the best place to learn. It’s where AMPscript is used most often and where you’ll see the biggest impact quickly.

How AMPscript Works: The Basics

Before we get into examples, let’s cover the core building blocks.

1. Declaring Variables

Variables store values you want to use later.

%%[
VAR @firstname
SET @firstname = AttributeValue("FirstName")
]%%

2. Outputting Values

To display a variable in your email:

%%=v(@firstname)=%%

3. Comments

Comments help you document your code.

/* This is a comment */

4. Conditional Logic

If/Else statements let you control what content appears.

%%[
IF @firstname == "" THEN
    SET @firstname = "there"
ENDIF
]%%

5. Functions

AMPscript includes hundreds of functions for:

  • String manipulation
  • Date handling
  • Data lookups
  • Math
  • Personalisation
  • API calls

You’ll see some of the most useful ones in a bit below.

Getting Started: Simple AMPscript Examples

Let’s walk through some beginner‑friendly examples you can use immediately.

Example 1: Basic Personalisation

This is the classic “Hello, FirstName” example — but written safely.

%%[
VAR @firstname
SET @firstname = AttributeValue("FirstName")

IF EMPTY(@firstname) THEN
    SET @firstname = "Customer"
ENDIF
]%%

<p>Hello %%=v(@firstname)=%%,</p>

Why this matters:

  • AttributeValue() prevents errors if the field doesn’t exist
  • EMPTY() handles nulls, blanks, and missing values
  • You avoid awkward greetings like “Hello ,”

Example 2: Personalised Product Recommendations

Let’s say you store a subscriber’s favourite category in a Data Extension.

%%[
VAR @category
SET @category = AttributeValue("FavouriteCategory")

IF @category == "Running" THEN
    SET @message = "We’ve picked out some new running gear just for you."
ELSEIF @category == "Yoga" THEN
    SET @message = "Here are some calming yoga essentials."
ELSE
    SET @message = "Here are some products we think you’ll love."
ENDIF
]%%

<p>%%=v(@message)=%%</p>

This is a simple example of dynamic content without needing a full dynamic content block.

Example 3: Date Formatting

Dates in Marketing Cloud often come in ugly formats. AMPscript can fix that.

%%[
VAR @renewalDate
SET @renewalDate = AttributeValue("RenewalDate")

SET @formattedDate = FormatDate(@renewalDate, "dd MMMM yyyy")
]%%

<p>Your subscription renews on %%=v(@formattedDate)=%%.</p>

Data Lookups: The Most Useful AMPscript Skill You’ll Learn

If you only learn one AMPscript skill, make it data lookups. They unlock almost everything.

Lookup Example: Pulling a Single Value

%%[
VAR @loyaltyPoints

SET @loyaltyPoints = Lookup(
    "LoyaltyPointsDE",
    "Points",
    "SubscriberKey",
    _subscriberkey
)
]%%

<p>You currently have %%=v(@loyaltyPoints)=%% points.</p>

This retrieves the subscriber’s points from a Data Extension called LoyaltyPointsDE.

LookupRows Example: Pulling Multiple Records

Useful for order history, event lists, or multi‑row data.

%%[
VAR @rows, @row, @i, @count

SET @rows = LookupRows("OrdersDE", "SubscriberKey", _subscriberkey)
SET @count = RowCount(@rows)

IF @count > 0 THEN
    FOR @i = 1 TO @count DO
        SET @row = Row(@rows, @i)
        SET @orderName = Field(@row, "ProductName")
]%%

<p>You ordered: %%=v(@orderName)=%%</p>

%%[
    NEXT @i
ENDIF
]%%

This loops through all orders for the subscriber and prints each one.

Dynamic Content with AMPscript

Dynamic content is one of the biggest reasons marketers learn AMPscript. It gives you complete control over what each subscriber sees.

Here are some practical patterns.

1. Dynamic Images

%%[
VAR @segment
SET @segment = AttributeValue("Segment")

IF @segment == "Beginner" THEN
    SET @image = "https://example.com/beginner.jpg"
ELSEIF @segment == "Advanced" THEN
    SET @image = "https://example.com/advanced.jpg"
ELSE
    SET @image = "https://example.com/default.jpg"
ENDIF
]%%

<img src="%%=v(@image)=%%" alt="Segmented Image">

2. Dynamic CTAs

%%[
VAR @cta

IF AttributeValue("IsVIP") == "True" THEN
    SET @cta = "Access your VIP offers"
ELSE
    SET @cta = "See this week’s deals"
ENDIF
]%%

<a href="https://example.com">%%=v(@cta)=%%</a>

3. Dynamic Blocks of HTML

AMPscript can output entire HTML blocks.

%%[
VAR @html

SET @html = "<div style='padding:20px;background:#f5f5f5;'>"
SET @html = Concat(@html, "<h2>Your personalised picks</h2>")
SET @html = Concat(@html, "<p>Based on your recent browsing.</p>")
SET @html = Concat(@html, "</div>")
]%%

%%=v(@html)=%%

Common AMPscript Use Cases (And How to Implement Them)

Let’s break down the most common real‑world scenarios where AMPscript shines.

1. Personalisation at Scale

Personalisation is the bread and butter of AMPscript.

Typical use cases:

  • Greeting subscribers by name
  • Showing personalised product categories
  • Displaying loyalty points
  • Tailoring messaging based on behaviour
  • Using fallback values when data is missing

AMPscript makes this safe, consistent, and scalable.

2. Data Lookups from Data Extensions

This is essential when:

  • You store data outside the sendable DE
  • You need multi‑row data
  • You want to enrich emails with transactional or behavioural data
  • You’re working with relational data models

AMPscript’s lookup functions are fast, reliable, and widely used across enterprise implementations.

3. Dynamic Content Logic

Dynamic content blocks are great — but they’re not always flexible enough.

AMPscript gives you:

  • Unlimited conditions
  • Nested logic
  • Multi‑variable decisions
  • Dynamic HTML generation
  • More control over fallback behaviour

If you’ve ever hit the limits of the drag‑and‑drop dynamic content tool, AMPscript is the answer.

4. Form Handling on CloudPages

AMPscript can:

  • Capture form submissions
  • Validate inputs
  • Write data to Data Extensions
  • Trigger emails
  • Redirect users

This is where AMPscript starts to feel like a lightweight backend language.

5. String and Date Manipulation

Real‑world examples:

  • Formatting dates for humans
  • Cleaning up messy data
  • Trimming whitespace
  • Extracting parts of strings
  • Building personalised URLs

These small touches make your emails feel polished.

Best Practices for Writing AMPscript

A few habits will save you hours of debugging.

1. Keep Your Script at the Top

Place your AMPscript block at the top of your email:

%%[
/* All your logic here */
]%%

Then reference variables throughout the email.

2. Use Meaningful Variable Names

Bad:

SET @x = AttributeValue("FN")

Good:

SET @firstName = AttributeValue("FirstName")

3. Always Use Fallbacks

Never trust your data blindly. If you have blank values in your DE, make sure you are using a fallback value,

4. Test with the “Validate” Tool

AMPscript errors can break an entire send. Always validate. This is a tool I love to use ampscript.io – AMPscript syntax validation and highlighting

5. Use the “View As” Feature

Test with real subscriber data whenever possible. And try a few, not just one.

A Simple AMPscript Template You Can Reuse

Here’s a clean starter template for any email:

%%[
/* Declare variables */
VAR @firstName, @segment, @points

/* Pull attributes */
SET @firstName = AttributeValue("FirstName")
SET @segment = AttributeValue("Segment")

/* Fallbacks */
IF EMPTY(@firstName) THEN
    SET @firstName = "there"
ENDIF

/* Lookup example */
SET @points = Lookup("LoyaltyPointsDE", "Points", "SubscriberKey", _subscriberkey)
]%%

<p>Hello %%=v(@firstName)=%%,</p>

<p>You currently have %%=v(@points)=%% loyalty points.</p>

%%[
IF @segment == "VIP" THEN
]%%
<p>Thanks for being one of our VIP customers.</p>
%%[
ENDIF
]%%

Copy it. Adapt it. Use it everywhere.

Final Thoughts: AMPscript Is a Skill Worth Learning

If you’re working in Salesforce Marketing Cloud, AMPscript is one of the highest‑value skills you can invest in. It bridges the gap between marketing and data, giving you the power to build personalised, dynamic, data‑driven experiences without relying on developers.

And the best part? You don’t need to be a programmer to learn it. You just need:

  • A few core concepts
  • Some practical examples
  • A willingness to experiment

This guide should give you everything you need to get started — and if you’re building your skills, keep an eye on ianjamieson.com. I’ll be publishing more AMPscript walkthroughs, deeper dives into common functions, and practical templates you can use in your own campaigns.

Happy Marketing, Ian