Prettying up AMPScript in emails

When you’re creating a dynamic email in Salesforce Marketing Cloud using AMPScript, things can get muddled up with non-HTML code real fast — so muddled it’s hard to recognize the basic layout of the email.  Sometimes it’s helpful to hide the AMPScript in a way that doesn’t affect the functionality. Here’s a trick that I use on a regular basis.

Hide your init block in with a styled DIV

I normally use these at the top of an email for initialization of variables, etc. Simply edit the content area to view the full code.

DO NOT use HTML comments to hide AMPScript. It will screw up the automatic text version of your email!

Checking lookupRow() results

Even though your email will pass validation in ExactTarget, if you don’t check for positive lookupRows() results with rowcount() before using the row() function, then you may end up with a run-time error that aborts your send.

These kinds of errors are a pain to track down in the SFMC platform. Save yourself some debugging/support call time.

Don’t do this:

%%[
var @rows, @row, @rowCount
var @lookupValue
set @lookupValue = "whee"

set @rows = LookupRows("DataExtensionName","LookupColumn", @lookupValue)

var @DEColumn1, @DEColumn2
set @row = row(@rows,1) /* get row #1 */
set @DEColumn1 = field(@row,"DEColumn1")
set @DEColumn2 = field(@row,"DEColumn2")

]%%

DEColumn1 is %%=v(@DEColumn1)=%%, DEColumn2 is %%=v(@DEColumn2)=%%

 

Do this:

%%[
var @rows, @row, @rowCount
var @lookupValue
set @lookupValue = "whee"

set @rows = LookupRows("DataExtensionName","LookupColumn", @lookupValue)
set @rowCount = rowcount(@rows)

if @rowCount > 0 then

var @DEColumn1, @DEColumn2
set @row = row(@rows,1) /* get row #1 */
set @DEColumn1 = field(@row,"DEColumn1")
set @DEColumn2 = field(@row,"DEColumn2")

]%%

DEColumn1 is %%=v(@DEColumn1)=%%, DEColumn2 is %%=v(@DEColumn2)=%%

%%[ else ]%%

No rows found

%%[ endif ]%%

AMPScript Lookup Examples

Here are a few ways to retrieve external data with AMPScript inside your email or landing page:

Lookup value of single column value from a certain row

Lookup multiple column values from a single row

Lookup multiple column values from multiple ordered rows

For further reference:



AMPScript Social Links

Need to share content in your Salesforce Marketing Cloud email? Here are some methods of building social sharing links using AMPScript inside the Salesforce Marketing Cloud platform. The link structure can be used in plain ole HTML and JavaScript also.

Facebook

%%[
var @fbTitle, @fbImage, @fbSummary, @fbURL, @fbPostURL
set @fbTitle = "this is the title"
set @fbImage = "https://www.google.com/images/srpr/logo3w.png"
set @fbSummary = "whee whoa whoops"
set @fbURL = "https://sprignaturemoves.com/"

set @fbPostURL = concat("https://www.facebook.com/sharer/sharer.php?s=100&p%5Btitle%5D=", urlencode(@fbTitle,1),"&p%5Burl%5D=",urlencode(@fbURL,1),"&p%5Bimages%5D%5B0%5D=",urlencode(@fbImage,1),"&p%5Bsummary%5D=",urlencode(@fbSummary,1))
]%%

Post to Facebook

NOTE: The https://www.facebook.com/sharer.php link doesn’t work on mobile devices. Let me know in the comments if you find a good solution for Facebook sharing on mobile without JavaScript.

Twitter

%%[
var @twText, @twPostURL
set @twText = "whee whoa whoops https://sprignaturemoves.com/"
set @twPostURL = concat("https://twitter.com/intent/tweet?source=webclient&text=",urlencode(@twText,1))
]%%

Tweet it

Pinterest

%%[
var @pinURL, @pinImageURL, @pinDesc, @pin
set @pinURL = "https://sprignaturemoves.com/"
set @pinImageURL = "https://www.google.com/images/srpr/logo3w.png"
set @pinDesc = "whee whoa whoops"
set @pin = concat("http://pinterest.com/pin/create/button/?url=",urlencode(@pinURL,1),"&media=",urlencode(@pinImageURL,1),"&description=",urlencode(@pinDesc,1))
]%%

pin it

Google Plus

%%[
var @gplusURL, @gplusLang
set @gplusURL = "https://sprignaturemoves.com/"
set @gplusLang = "en-US"
set @gplusLink = concat("https://plus.google.com/share?url=",urlencode(@gplusURL,1),"&hl=",urlencode(@gplusLang,1))
]%%

[+1]

For further reference:
concat
urlencode
redirectto
Google Developer – Share links
Google Developer – Available languages