How to fetch code snippet with AMPScript

Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks
1 minute read

If you have ever experienced troubles when referring to your content block within another resource, either an email template or a cloud page, using ContentBlockBy AMPScript functions, I will show you a hopefully better way to include your snippets in your content.

Dynamic content creation and personalization within Salesforce Marketing Cloud, can often grapple developers with the challenge of making their AMPScript code not only functional but also easily maintainable. One of the key considerations, for me and hope for many others, is how to reference content blocks in a way that is both human-readable and robust against changes.

ContentBlockByName

This function retrieves content from a specified Content Block, with an optional Impression Region wrapping.

Arguments: ContentBlockByName(path, impressionRegion, throwError, defaultContent, statusCode)

Parameters:

  1. path (String, required): Full path of the content block to retrieve.
  2. impressionRegion (String, optional): Name of the Impression Region associated with this Content Area.
  3. throwError (Boolean, optional): Returns an error if the Content Area cannot be found.
  4. defaultContent (String, optional): Default content returned if an error occurs during retrieval.
  5. statusCode (Number, optional): Numeric status code; 0 for success, -1 if content is not found or empty.
%%[

set @contentBlockName = "Content Builder\mycontentblock"
set @content = ContentBlockByName(@contentBlockName, "Greeting", 0, "", @statusCode)

if @statusCode == 0 then
  output(v(@content))
endif
]%%
ContentBlockById


Same function with identical arguments as the previous function, but instead of a path, we have to provide the ID of the content block.

%%[

set @contentBlockID = 12345
set @content = ContentBlockByName(@contentBlockID, "Greeting", 0, "", @statusCode)

if @statusCode == 0 then
  output(v(@content))
endif
]%%
ContentBlockByKey

Last but not least, we can request a content block by its external key, which we can modify to our needs, making it readable and understandable for any code readers passing by.

%%[

set @contentBlockKey = 'mycontentblock'
set @content = ContentBlockByKey(@contentBlockKey, "Greeting", 0, "", @statusCode)

if @statusCode == 0 then
  output(v(@content))
endif
]%%
So which one is the best?


I personally, and hopefully many others, find the best function to use is ContentBlockByKey for several reasons:

  • you can move the block around without breaking the template as opposed to ContentBlockByName
  • human-readable text in your code without the need for any lookups as opossed to ContentBlockById

Exploring the Dynamic World of Email Marketing: Diverse Template Designs in Salesforce Marketing Cloud
Salesforce Marketing Cloud, SFMC Tips & Tricks

Proof multiple template variants in SFMC

2 minutes read

When working with dynamic email templates that can generate multiple unique variants of the email, you are most probably tasked to proof all the variants to your stakeholder for review. Create data extension When proofing email templates, traditionally, we select subscribers from production or testing data extensions used in the actual send. This process involves […]

Continue reading
Adobe Campaign Classic tips
ACC Tips & Tricks, Adobe Campaign, Marketing Automation

Pass custom variables to delivery template

2 minutes read

In your delivery, you can use different types of data that can be utilized for personalized content for your customers. To add a custom variable directly to the delivery from the workflow, you will need to Create delivery variables First, you need to create placeholders for delivery variables within your delivery template. These placeholders serve […]

Continue reading
Adobe Campaign Classic tips
ACC Tips & Tricks, Adobe Campaign, Marketing Automation

Add content variation to the proof subject

1 minute read

When dealing with multiple variants defined within one delivery template featuring dynamic content blocks, stakeholders most likely want to see proofs for each variant. To enhance readability in their mailbox, I tend to add the email variant to the subject line. A simple solution, similar to what we shown you when using salesforce marketing cloud […]

Continue reading
Top 5 SFMC tips
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

The Top 5 Stress-Busting Tips I have Learnt

3 minutes read

Working with any marketing automation platform, especially Salesforce Marketing Cloud, can provide a great user experience and a sense of accomplishment. However, it can also entail many stressful hours dedicated to solving seemingly easy issues. Here are my top 5 tips that made my daily work less stressed and saved me lots of cursing this […]

Continue reading
Adobe Campaign Classic tips
ACC Tips & Tricks, Adobe Campaign, Marketing Automation

Proof Email Template for Multiple Variants

1 minute read

Imagine having to proof all the possible variants of a delivery template, where there could be numerous end variations. How can we effectively proof such an email template? There is a simple yet powerful workaround for proofing all the variants, and here’s how to achieve it: Moreover, to differentiate between variations, you can include your […]

Continue reading
Salesforce Marketing Cloud Tips
Marketing Automation, Salesforce Marketing Cloud

Fix email preview: Out Of Memory Exception

1 minute read

Sometimes, it happens that when you try to preview your email in Salesforce Marketing Cloud, you could encounter a system.OutOfMemoryException. But don’t worry; it’s not your laptop’s fault, even if it’s as old as mine. This error typically means that your template personalization script is overly complicated and needs some attention. You have two options: […]

Continue reading
Salesforce Marketing Cloud Rant
Marketing Automation, Salesforce Marketing Cloud, SFMC Rant

SFMC RANT: Inline AMPScript mystery

3 minutes read

Working with Salesforce Marketing Cloud, I’ve learned that you often need to try different workarounds until you achieve the desired result. To succesfully solve problems, it all depends on your experience and patience. Recently, I did a significant amount of work with AMPScript and personalization and wrote many tips during these days. It seems that […]

Continue reading
Salesforce Marketing Cloud Tips
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

Proof email was previewed but not received

2 minutes read

There are many possible issues, and I will try to list all those I have come across during my times when I wondered where my email is. Contact is unsubscribed If the contact you are trying to preview an email with is on one of the global unsubscribe lists, or has unsubscribed or bounced status […]

Continue reading
Salesforce Marketing Cloud Tips
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

Track links that are AMPScript variables

1 minute read

When you save links as part of an HTML code in an AMPScript variable, such as a paragraph containing a link to a page, you may face challenges in tracking these links. Salesforce offers a great feature that allows tracking of such links using the “httpgetwrap” inserted right before the URL protocol. When dealing with […]

Continue reading