Skip to main content

AMPscript snippets

Debug your AMPscript error || A must have Try Catch for Complex Cloud pages
<script runat="server" type="text/javascript">
Platform.Load("Core", "1.1.1");
try {
</script>
%%[
/* AMPscriptHere */
]%%
<script type="text/javascript" runat="server">
} catch (e) { Write("<br>AMPscript Error Message: " + e); }
</script>
Re-Usable snippet to debug Email CTA --> CloudPage usecases
%%[
/* These default system paramters works in both Email and Cloud page when used along with CloudPagesURL() */
SET @_subscriberkey = AttributeValue(_subscriberkey)
SET @emailaddr = AttributeValue(emailaddr)
SET @emailname_ = AttributeValue(emailname_)

/* This RequestParamters() works only in Cloud page */
SET @URL_Param1 = RequestParameter('Param1')
SET @URL_Param2 = RequestParameter('Param2')
]%%

_subscriberkey = %%=v(@_subscriberkey)=%% <br>
emailaddr = %%=v(@emailaddr)=%% <br>
emailname_ = %%=v(@emailname_)=%% <br>
URL_Param1 = %%=v(@URL_Param1)=%% <br>
URL_Param2 = %%=v(@URL_Param2)=%% <br>
AMPscript String Reverse
%%[
var @name, @rev_name
SET @name = 'vishal'

FOR @i = Length(@name) DOWNTO 1 DO
SET @rev_name = Concat(@rev_name,Substring(@name,@i,1))
NEXT @i

Output(Concat('Given name = ',@name)) //vishal
Output(Concat('Reversed = ',@rev_name)) //lahsiv
]%%
Finding Greatest and Lowest of 3 numbers
%%[
/* Converting strings to Numbers */
set @n1 = add('10',0)
set @n2 = add('10.5',0)
set @n3 = add('11',0)

set @greatestN = iif(@n1 > iif(@n2 > @n3, @n2, @n3), @n1, iif(@n2 > @n3, @n2, @n3))
set @lowestN = iif(@n1 < iif(@n2 < @n3, @n2, @n3), @n1, iif(@n2 < @n3, @n2, @n3))
]%%

n1: %%=v(@n1)=%% <br>
n2: %%=v(@n2)=%% <br>
n3: %%=v(@n3)=%% <br>
Lowest is: %%=v(@lowestN)=%% <br>
Greatest is: %%=v(@greatestN)=%% <br>
Surprized! yes this is AMPscript
<script runat='server' language='ampscript'>
Output(Concat('Welcome ','to SFMC-NINJA'))
</script>
Email Salutation - snippet
%%[
set @FirstName = AttributeValue("FirstName")
set @Salutation = iif(length(@FirstName) > 1, ProperCase(@FirstName), "Customer")
]%%
Using Commas in Indian and International Number System || Ex: Adding comma at 4th position from right
%%[
SET @price = "10047654 "
SET @sl = Length(@price)
/* Adding comma at 4th position from right */
SET @l = Substring(@price,1,Subtract(@sl,4))
SET @r = Substring(@price,Subtract(@sl,3),4)
SET @FS = Concat(@l,',',@r)
]%%

String Length = %%=v(@sl)=%%
LeftString = %%=v(@l)=%%
RightString = %%=v(@r)=%%
FS = %%=v(@FS)=%%
Excluding code execution - SendPreview and test send
%%[ IF (_messagecontext != "PREVIEW" AND _isTestSend != true) THEN
CreateSalesforceObject(.....
ENDIF
]%%
Hiding the View As Web Page - when viewed on browser
Hiding the View As Web Page - when viewed on browser
%%[IF _messagecontext != 'VAWP' THEN]%%
<table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" style="background-color:#ffffff;" bgcolor="#ffffff">
<tr>
<td valign="middle" align="center" style="font-family: Arial, sans-serif; font-size:12px; color:#000001">
To view this email as a web page, click <a href="%%view_email_url%%" target="_blank" style="text-decoration:underline; color:blue;">here</a>.</td>
</tr>
</table>
%%[ENDIF]%%
Transmission of parameters to CloudPage as ENCRYPTED method vs VISIBLE parameters
//Parameters will be encrypted in URL [Example: xxxx.com?qs=2f6e3f80d6017db21xxxxxxxxxxx]
<a href="%%=RedirectTo(CloudPagesURL(xxx,'subkey',_subscriberkey))=%%">Go to cloud page</a>
// On cloud page : below works both for GET and POST
%%[ SET @subkey = RequestParameter("subkey") ]%%


//Parameters will be visible in the URL [?name=VishalKumarCV]
<a href="%%=Concat(RedirectTo(CloudpagesURL(xxx)),'&name=',@varname)=%%">Go to cloud page</a>
// On cloud page : below works only for GET method
%%[ set @name = QueryParameter("name") ]%%