Salesforce Marketing Cloud: AmpScript Basics
Ampscript in Marketing Cloud
AMPscript is a scripting language that is embedded within text emails, HTML emails, SMS messages, landing pages in the marketing cloud.
Variables:- Variables in amp script are used to store List or DE data for use. As we use in apex classes.
Don't miss to check out: All About Salesforce Marketing Cloud, Email studio and Content Builder
Syntax:
This the syntax for declaring variables and syntax:
%%[ var @firstName set @firstName = "Test" %%= v(@firstName) =%% ]%%
INLINE Ampscript :
We can write inline Ampscript as -
%%= LOWERCASE(firstName) =%%
AmpScript Block
AmpmBlock can be written as-
[%%LOWERCASE(firstName)%%]
Tags:
Amp Script also have opening and closing tab as in HTML
<script runat=server language=ampscript> Lowercase(firstName) </script> <script runat=server language=ampscript> IF @region == 'Bangalore' THEN SET @country = 'India' ELSEIF @region == 'California' THEN SET @country = 'USA’ ENDIF </script>
Example:
%%[FOR @count = '01' TO @Position = @rowCount DO ]%% SET @FirstName = FirstName %%[NEXT @count]%%
Functions in Ampscript:
AddObjectArrayItem:- This function is used for adding attributes of object to retrieve the request(ID & name).
CreateObject:- It is used to instantiate specified marketing cloud API objects.
set @var = CreateObject("object")
InvokeCreate:- This function is used to execute created objects of specified marketing cloud API.Also, this function returns status code, message, and error code.
set @createStatusCodevar = InvokeCreate(@var, @createStatusMessage, @createErrorCode)
InvokeExecute:- This function is used to execute created objects of specified marketing cloud API, and it returns ‘RequestID value and it runs asynchronously.
set @invokeStatusObjVar = InvokeExecute(@var,@invokeStatusMessage,@invokeRequestId)
InvokeRetrieve:- It retrieves the RetrieveRequest of instigated marketing cloud API. This function returns a ‘status message’ as well as ‘RequestID’ value.
set @dataExtensionFieldsVar = InvokeRetrieve(@var, @varStatus, @varRequestID)
InvokeUpdate:- This function executes an update on marketing cloud Api object instantiated and it returns error, status code and message.
set @updateStatusCodevVAr = InvokeUpdate(@Var, @updateStatusMessagevar, @updateErrorCodevar, @updateOptionsvar)
RaiseError:- This function is used to enable suppression of an email send.
RaiseError("No offer is available")
SetObjectProperty:- This function is used to set the property of the object, created by ‘CreateObject()’ function.
set @varSub = CreateObject("Subscribers") SetObjectProperty(@varSub, "EmailAddress", "[email protected]") SetObjectProperty(@varSub, "SubscriberKey", "6658") SetObjectProperty(@varSub, "Status", "Inactive")
Lookup in AMPscript:- It returns required value from data extension.
Example:
%%=Lookup('postalcode','city','PostalCode',3562)=%%
Lookup(ordinal 1, ordinal 2, ordinal 3,ordinal 4)
ordinal 1: Name of DE from which we want to retrieve
ordinal 2: Column name from which we want to retrieve
ordinal 3:Name of column used to identify row contains value
ordinal 4:Matched value
LookupOrderedRows:-
Returns numbers of rows.
Example:
Set @row2 = LookupOrderedRows("Bikes",4,"HorsePower Break","MG1",Field(@Bikedata,"MG1"))
LookupOrderedRows(ordinal 1,ordinal 2,ordinal 3,ordinal 4,ordinal 5)
ordinal 1:-Name of DE from which we want to retrieve
ordinal 2:-Number of rows wants to return
ordinal 3:-Order of filed either ASC or Dec
ordinal 4:- Field used for where clause
ordinal 5:- Field used for where clause
Check out another amazing blog by Manish here: Best Practices in Salesforce to Write Test Classes for 100% Code Coverage
LookUpRows:
It returns a set of rows from a data extension where the field matches the given value.
Example:
LookupRows('ZipCode','city',Hometown City)
When a city row matches with Hometown City , it returns zipcode rows.
LookupRows(ordinal 1, ordinal 2,ordinal 3)
ordinal 1: Name of DE from which we want to retrieve
ordinal 2: Name of column used to identify rows
ordinal 3: value to match with row that we want to return
Reference: Salesforce Developers