A personalization string gets you “Hi Sarah.” AMPscript gets you Sarah’s nearest store, her last order, a price in her currency and a different hero image because she’s a loyalty member.
This guide covers the syntax you’ll use every day, 8 copy-ready examples, how to debug, and what changed now that AMPscript also runs in Marketing Cloud Next.
What AMPscript is
AMPscript is Salesforce Marketing Cloud’s scripting language for personalizing content at send time. Salesforce’s AMPscript Function Reference describes it as a language for Marketing Cloud Engagement and Marketing Cloud Next that connects your customer data with your marketing content.
In Marketing Cloud Engagement, you can use it in emails, SMS, CloudPages and landing pages. If you’re new to it, Trailhead’s AMPscript for Nondevelopers module is a gentle start.
Syntax basics
AMPscript has 3 ways to appear in content:
Form | Use | Example
|
|---|---|---|
Code block | Logic and variables, no output | %%[ SET @name = “Sarah” ]%% |
Inline | Output a value where it sits | %%=v(@name)=%% |
Personalization string | Output a field directly | %%FirstName%% |
A few rules to remember:
- Variables start with @ and are declared with VAR, then set with SET
- Functions and keywords aren’t case-sensitive, but be consistent
- Strings use single or double quotes
- Comments go inside /* */ in a code block
- Code runs top to bottom, so set variables before you output them
8 examples you can adapt
1. A safe first name with a fallback
%%[
VAR @first
SET @first = AttributeValue(“FirstName”)
IF Empty(@first) THEN
SET @first = “there”
ENDIF
]%%
Hi %%=ProperCase(@first)=%%,
AttributeValue() returns an empty value instead of an error when the field isn’t in the sending audience, which makes it safer than a bare personalization string.
2. Look up a value from another data extension
%%[
VAR @store
SET @store = Lookup(“Stores”, “StoreName”, “StoreID”, AttributeValue(“PreferredStoreID”))
]%%
Your nearest store: %%=v(@store)=%%
3. Conditional content by tier
%%[
VAR @tier, @offer
SET @tier = AttributeValue(“LoyaltyTier”)
IF @tier == “Gold” THEN
SET @offer = “20% off this weekend”
ELSEIF @tier == “Silver” THEN
SET @offer = “10% off this weekend”
ELSE
SET @offer = “Free delivery this weekend”
ENDIF
]%%
%%=v(@offer)=%%
4. Loop through recent orders
%%[
VAR @rows, @row, @i
SET @rows = LookupOrderedRows(“Orders”, 3, “OrderDate DESC”, “SubscriberKey”, _subscriberkey)
FOR @i = 1 TO RowCount(@rows) DO
SET @row = Row(@rows, @i)
]%%
<p>%%=Field(@row, “ProductName”)=%% on %%=FormatDate(Field(@row, “OrderDate”), “d MMM yyyy”)=%%</p>
%%[ NEXT @i ]%%
5. Format a price for the reader’s locale
%%=FormatCurrency(AttributeValue(“CartTotal”), “en-GB”)=%%
6. Build a tracked, personalized link
<a href=”%%=RedirectTo(Concat(‘https://example.com/account?id=’, _subscriberkey))=%%”>View your account</a>
Wrap personalized URLs in RedirectTo() so Marketing Cloud can track the click.
7. Pick an image by category affinity
%%[
VAR @cat
SET @cat = Lowercase(AttributeValue(“TopCategory”))
IF Empty(@cat) THEN SET @cat = “default” ENDIF
]%%
<img src=”https://example.com/heroes/%%=v(@cat)=%%.jpg” alt=”Picked for you”>
8. Stop a send for bad data
%%[
IF Empty(AttributeValue(“Email”)) OR Empty(AttributeValue(“Country”)) THEN
RaiseError(“Missing email or country”, true)
ENDIF
]%%
RaiseError() with true skips that subscriber and continues the send, which is safer than sending broken content.
AMPscript in Marketing Cloud Next
Marketing Cloud Next (the Growth and Advanced editions on the core platform) originally used Handlebars for personalization. Salesforce added AMPscript in the Summer ’26 release, and the function docs now show separate support columns for Engagement and Next.
Support is partial, and some functions behave differently. The Lookup() reference shows the pattern:
- It became available in Marketing Cloud Next in Summer ’26 (API version 67.0)
- In Engagement, you name a data extension, while in Next you give the API name of a marketing object
- In Next, an odd number of search arguments returns an error
Functions tied to Engagement-only features, such as CloudPages, social forwarding and data extension writes, aren’t available in Next. Check each function’s support column before you reuse old code.
AMPscript or Handlebars?
In Marketing Cloud Next, you have both. Salesforce’s Handlebars for Marketing Cloud Next guide describes built-in helpers for working across data sources, filtering, sorting and applying logic.
Situation | Better choice
|
|---|---|
New templates built only for Marketing Cloud Next | Handlebars |
Marketers editing templates without a developer | Handlebars, plus content variations and merge fields |
Porting existing Engagement emails | AMPscript, where the functions are supported |
Complex logic in Engagement (CloudPages, data writes, API calls) | AMPscript in Engagement |
If you’re planning a move from Engagement to Next, inventory your AMPscript first. Code that writes to data extensions, calls APIs or runs on CloudPages usually needs redesigning, not copying. Our guide to Marketing Cloud Growth vs Advanced covers the wider edition choice.
Debugging checklist
- Preview with real subscribers. Use subscriber preview with records that have empty, short and unusual values.
- Output variables temporarily. Print %%=v(@var)=%% next to the logic while testing, then remove it.
- Check field names. A typo in a data extension or column name is the most common failure.
- Check data types. Dates stored as text break FormatDate() and comparisons.
- Guard every lookup. Wrap outputs in IF NOT Empty() so a missing row doesn’t leave a blank sentence.
- Send a test to a seed list. Some functions only behave correctly at send time, not in preview.
- Keep shared logic in a content block. Reuse it with ContentBlockByKey() rather than copying it into every email.
Performance and maintenance
Each lookup runs for each subscriber. A large send with 10 lookups per email does a lot of work, so pull what you need into the sendable data extension with a SQL query activity when you can.
Comment your code, name variables clearly, and keep a library of tested snippets. The next person to edit the email will thank you.
Where AMPscript fits in a program
AMPscript decides what each message says. Journey Builder decides who gets which message and when, and Marketing Cloud Personalization can decide content in real time at open.
All 3 depend on clean, connected data. In our prospecting tools case study, the first win came from getting enrichment data into 1 Salesforce record, which is exactly what reliable personalization needs.
Frequently asked questions
What is AMPscript used for?
AMPscript personalizes Marketing Cloud content at send time. It’s used to look up related data, show conditional content, format dates and prices, loop through records like recent orders, build tracked links and, in Engagement, power CloudPages and landing pages.
Does AMPscript work in Marketing Cloud Next?
Yes, since the Summer ’26 release, but only for a subset of functions. Salesforce’s function reference marks each function as supported or not in Engagement and in Next, and some, like Lookup(), work differently in Next.
Is AMPscript hard to learn?
The basics take a day or 2 for anyone comfortable with spreadsheets or simple code. Lookups, loops and debugging take longer. Trailhead’s AMPscript for Nondevelopers module is a good free starting point.
What’s the difference between AMPscript and SSJS?
AMPscript is built for personalizing content and is simpler for most email work. Server-side JavaScript (SSJS) runs in Marketing Cloud Engagement for heavier processing, API calls and automation scripts. Many teams use AMPscript in emails and SSJS in automations.
Get help with AMPscript and Marketing Cloud builds
HyphenX builds and maintains Marketing Cloud emails, Journey Builder programs and personalization, including AMPscript audits before a move to Marketing Cloud Next. Talk to our Salesforce Marketing Cloud services team, or hire a Marketing Cloud specialist for ongoing work.
