Fundamental AmpScript Concepts in Salesforce Marketing Cloud

AmpScript within the Marketing Cloud

AMPscript serves as an embedded scripting language employed in text emails, HTML emails, SMS messages, and landing pages within the Marketing Cloud.

Variables: In AMPscript, variables are utilized to store List or Data Extension (DE) data for later use, much like how we utilize them in Apex classes.

Syntax:

This represents the syntax for variable declaration:

%%[ 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 ]%% &nbsp;&nbsp;&nbsp;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", "abc@mailenator.com")
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: 

<span style="background-color: var(--bb-content-alternate-background-color); color: rgb(77, 92, 109); font-family: &quot;Courier New&quot;, courier, &quot;Lucida Sans Typewriter&quot;, &quot;Lucida Typewriter&quot;, monospace; font-size: 1rem; letter-spacing: -0.24px;">Set @row2 = LookupOrderedRows("Bikes",4,"HorsePower Break","MG1",Field(@Bikedata,"MG1"))</span>

LookupOrderedRows(ordinal 1,ordinal 2,ordinal 3,ordinal 4,ordinal 5)

ordinal 1:-Name of DE from which we want to retrieveordinal 2:-Number of rows wants to returnordinal 3:-Order of filed either ASC or Decordinal 4:- Field used for where clauseordinal 5:- Field used for where clause

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