AMPscript Guide: Syntax, Examples & Functions
Learn AMPscript for Marketing Cloud Engagement with a beginner path, function examples, Data Extension lookup guidance, date formatting, and tracked email links.
AMPscript is a server-side scripting language for personalizing content in Salesforce Marketing Cloud Engagement. Use it to work with contextual values, look up Data Extension data, and choose what content to display. This guide gives you a starting path through the language and routes to function examples and practical authoring tips.
This page focuses on Marketing Cloud Engagement. Working in Marketing Cloud Next? Use the separate Next AMPscript reference to check that platform’s support rather than assuming the same functions and behavior apply.
This page collects general AMPscript authoring guidance. For per-function reference pages — signature, argument handling, edge cases, and the test script behind each claim — see the AMPscript function reference.
A beginner learning path
Learn the building blocks in this order. The language links below lead to the existing community reference; function links lead to examples on this site.
- Recognize the syntax. Start with AMPscript syntax, then compare script blocks with inline expressions. Blocks group statements; inline expressions place a function’s result in the content.
- Store a value and display it. Read about declaring variables and setting variables, then try the small example on the v function page. This separates assigning a value from displaying it.
- Choose between content branches. Follow conditional statements for
IF,ELSE, andENDIF. Use the Empty reference to understand empty-value checks before choosing fallback content. - Repeat a block. Read process loops for
FORandNEXT. Then explore the LookupRows examples when you are ready to work with multiple matching Data Extension rows.
Find a function for your task
- Personalization: begin with AttributeValue to read a contextual value, Empty to check whether it is empty, and v to display a value. The AttributeValue page explains why a CloudPage and an email send do not have the same subscriber context.
- Data Extension lookups: use the lookup family overview below to choose a single-value, rowset, or sorted-rowset reference.
- Date formatting: see FormatDate for date and time patterns and locale options.
- Tracked email links: follow dynamic trackable links and the RedirectTo reference when your destination URL comes from a variable.
Read the runtime evidence
The AMPscript function reference distinguishes catalog entries from functions marked Verified. Verified pages describe observed behavior from live Engagement CloudPage tests and include the supporting examples or test scripts. A catalog entry alone is not runtime evidence; read each page’s context and limitations before applying a result to your own content.
Practical authoring tips
First published 2019-03-03 in the SFMC Cookbook. Ported here for sfmc.guide.
Hide your code
When AMPscript is inserted into either an email or a CloudPage it usually clutters up the preview window with code. To avoid this, wrap your code in something that will not render:
<div style="display:none">
%%[
// your code here
]%%
</div>
Why not something that does not render out of the box, like <script>...</script> or <style>...</style>? SFMC strips every <script> tag out of emails without warning. Both tags might also trigger reformatting that silently breaks your code.
Why? Non-developers just get distracted by the code.
Why? Showing lots of code during a demo in the preview window distracts from the actual email content and costs you time explaining it again and again.
Why? Hiding the code shrinks the content block to its minimum height, making the right block easier to select because scrolling happens less often.
Data Extension lookups
The AMPscript Lookup family is documented individually, each with its own reference page:
- Lookup — single field of the first matching row, case-insensitive
- LookupRows — full rows, case-insensitive
- LookupRowsCS — full rows, case-sensitive
- LookupOrderedRows — sorted full rows, case-insensitive
- LookupOrderedRowsCS — sorted full rows, case-sensitive
Dynamic trackable links
Building link URLs from variables has a tracking pitfall — use RedirectTo rather than a hand-built <a> tag. See RedirectTo.
Other resources
- ampscript.guide — community AMPscript function reference