Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

How to fetch code snippet with AMPScript

How to fetch code snippet with AMPScript

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

Oh hi there 👋
I have a FREE e-book for you.

Sign up now to get an in-depth analysis of Adobe and Salesforce Marketing Clouds!

We don’t spam! Read our privacy policy for more info.

#AMPScript #cloud page #email template #programming
Marcel Szimonisz
Marcel Szimonisz
MarTech consultant As a marketing automation consultant, I specialize in problem-solving, process automation, and driving innovation for clients' marketing platforms.

I hold certifications in Adobe Campaign v6 (3x certified) and Salesforce Marketing Cloud (5x certified), as well as 1x Salesforce Associate certified.

Moreover, I serve as a community advisor for Adobe Campaign, providing expert insights and guidance.

Beyond my professional pursuits, I explore various programming languages, CMSs, and frameworks, enhancing my technical expertise and staying at the forefront of industry advancements.
Take a look at our subscription offering in case you are looking for a Marketing Automation Consultant.

Leave a comment

Your email address will not be published. Required fields are marked *

Similar posts that you may find useful

The image is a wide-format representation of a modern coding environment, specifically focused on the latest ES6 JavaScript features. It prominently displays a computer screen with a code editor, where JavaScript code is shown with highlights on ES6 features such as arrow functions, classes, and template literals. The code is adorned with vibrant syntax highlighting, making elements like code brackets and semi-colons stand out, emphasizing the JavaScript language. The background is infused with digital and technological elements like binary code patterns and abstract tech-inspired graphics, symbolizing innovation in coding. The overall color scheme is dynamic and lively, reflecting the advanced and cutting-edge nature of ES6 in the realm of web development.
Adobe Campaign, Marketing Automation

Enable newest ES6 JavaScript features

2 minutes read

You may have noticed, when working with the tool for some time, that the JavaScript used around the Adobe Campaign Classic platform is not the newest version. Adobe Campaign Classic relies on ES5 JavaScript, released in 2012. This may cause some first world problems for those accustomed to modern features like Array.indexOf, Array.find, classes, object […]

Continue reading
SFMC tips and tricks
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

Error updating filter and new data extension

less than a minute read

If you’re using Email Studio in Salesforce Marketing Cloud and receive an ‘Error: updating filter’ message when creating a new Data Extension, it’s likely because the name you’ve chosen already exists. This error message can be misleading, but a simple solution is to rename the Data Extension with a more unique identifier. Make sure to […]

Continue reading
Adobe Campaign post
Adobe Campaign, Marketing Automation

How to Track dynamic links

3 minutes read

Some email campaign send outs can have links dynamically rendered in the content. Such links are not visible by Adobe Campaign personalization engine and thus will not be tracked. The good thing is that we can use a workaround to make dynamic links trackable and also on top of all workarounds Adobe itself has provided […]

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

Journey validation fails for no reason

less than a minute read

There is an error with starting the journey, specifically related to the email template, but it gives you no explanation whatsoever. I’m sure there can be many problems with the template that prevent the journey from being started. One of the problems I have experienced most recently is that the template has not been approved. […]

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

Power of AMPScript Functions in SSJS

3 minutes read

Have you ever wondered, while working with SSJS, if there was a function in SSJS similar to the one you use in AMPscript? What if I told you that you can bring native AMPscript functions to SSJS? Today, I will show you how to incorporate the best features from both scripting worlds into SSJS automation, […]

Continue reading