AMPscript is a scripting language used in Salesforce Marketing Cloud to personalize and enhance marketing communications, such as emails and text messages. Functions in AMPscript are code blocks that perform specific tasks, such as data manipulation, mathematical calculations, and text formatting. Below, we will explore the general structure of an AMPscript function, different types of functions, and examples of some key functions.
The general structure of a function in AMPscript is as follows:
%%[ FunctionName(Argument1, Argument2, ...) ]%%
Lookup:
Retrieves a value from a Data Extension.
LookupRows:
Retrieves multiple rows from a Data Extension based on specified criteria.
RowCount:
Returns the number of rows in a Data Extension.
Row:
Returns a specific row from a Data Extension.
Concat
: Concatenates text strings.
ProperCase
: Converts the first letter of each word to uppercase.
UpperCase
: Converts text to uppercase.
LowerCase
: Converts text to lowercase.
Add
: Adds two numbers.
Subtract
: Subtracts two numbers.
Example: SET @subtotal = Subtract(@total, @discount)
Multiply
: Multiplies two numbers.
Example: SET @totalToPay = Multiply(@unitPrice, @quantity)
Divide
: Divides two numbers.
Example: SET @average = Divide(@sum, @count)
Mod
: Calculates the remainder of the division of two numbers.
Example: SET @remainder = Mod(@number, 2)
Random
: Generates a random number within a range.
Example: SET @randomNumber = Random(1, 100)
Here are examples of some specific functions in AMPscript:
String Concatenation Example:
%%[ SET @firstName = "John" SET @lastName = "Doe" SET @fullName = Concat(@firstName, " ", @lastName) ]%%
Proper Case Conversion Example:
%%[ SET @text = "hello world" SET @properText = ProperCase(@text) ]%%
Mathematical Calculation Example:
%%[ SET @price = 100 SET @discount = 20 SET @finalPrice = Subtract(@price, @discount) ]%%
These are just basic examples, and there are many other functions in AMPscript that you can explore to customize your marketing communications. Each function has its own syntax and usage options, so it's advisable to consult the official Salesforce Marketing Cloud documentation for specific details on each function and how to use it effectively.
Remember that AMPscript is a powerful tool for message customization and data interaction, so it can make a significant difference in your marketing campaigns when used correctly.
If you liked this chapter of the Ampscript for Marketers course, you might be interested in reading: