Introduction

AMPscript is a proprietary scripting language for Salesforce Marketing Cloud, enabling dynamic content personalisation and advanced data manipulation. This cheat sheet provides essential syntax, functions, and best practices for creating personalised marketing content.

Syntax Basics

Delimiters

Enclose AMPscript within double percentage signs and square brackets:

%%[ /* Your AMPscript code here */ ]%%

Variables

Declare variables using the var keyword:

%%[ var @variableName ]%%

Functions

Perform tasks like data retrieval or manipulation:

%%[ FunctionName(arguments) ]%%

Output

Display variable values using the v function:

%%=v(@variableName)=%%

Comments

Use /* comment */ for single-line and multi-line comments.

Common Functions

Data Retrieval

  • Lookup("DataExtension", "ColumnName", "LookupColumnName", "LookupValue")
  • LookupRows("DataExtension", "LookupColumnName", "LookupValue")

Data Manipulation

  • Concat(String1, String2, ...)
  • Uppercase(String)
  • Lowercase(String)
  • Substring(String, StartIndex, Length)

Date and Time

  • Now()
  • DateAdd(Date, Number, Interval)
  • FormatDate(Date, "Format")

Conditional Statements

IF

%%[ if Condition then
    /* Code if true */
else
    /* Code if false */
endif ]%%

IIF

IIF(Condition, ValueIfTrue, ValueIfFalse)

Best Practices

  1. Descriptive Variable Names: Use clear names for readability.
  2. Avoid Nested Functions: Simplify code to make it readable.
  3. Use Comments: Explain complex sections.
  4. Test Thoroughly: Ensure code works before deployment.

Advanced Tips

Error Handling

Use RaiseError to manage errors:

%%[ if Condition then
    RaiseError("Error message")
endif]%%

Combining AMPscript with SSJS

For advanced functionality:

<script runat="server">
    Platform.Load("Core", "1.1.1");
    var rows = Platform.Function.LookupRows("DataExtension", "LookupColumnName", "LookupValue");
    var result = "";
    for (var i = 0; i < rows.length; i++) {
        result += rows[i].ColumnName + "<br>";
    }
</script>
%%=v(@result)=%%

Sample Use Cases

  1. Personalization: Insert dynamic content based on subscriber attributes.
  2. Data Manipulation: Modify and format data before displaying it.
  3. Conditional Logic: Customize content based on specific conditions.

Putting It All Together

Start with your code block to set variables:

%%[
SET @SubKey = _subscriberkey
SET @Fname = FirstName
SET @Created = Lookup('Customers', 'CreatedDate', 'SubscriberKey', @SubKey)
SET @Sender = "Your local account manager"
]%%

Display variables within the content:

Hi %%=ProperCase(@Fname)=%%,

Thank you for being a valued customer with us since %%=Format(@Created, 'YYYY')=%%!

Regards,
%%=v(@Sender)=%%

Conclusion

With this AMPscript cheat sheet, you have a quick reference to help you create dynamic, personalised content for Salesforce Marketing Cloud. Follow best practices, experiment with different functions, and elevate your email marketing campaigns.

Tags:

AMPscript