3

Use case: Through automation studio, a script activity is use to call and reference a content area through the "ContentAreaByName" function. That content area contains AMPscript that performs specific functions. This idea come from and was detailed through this thread:

Using AMPscript with SSJS Activities

The process works great, but with classic content nearing retirement I would love to be replicate this with Content Builder. The issue is that whenever I build a Content Block instead of Content Area, and call either the "ContentBlockByName" or "ContentBlockById" functions the script errors every time. The AMPscript is validated and works as a Content Area, but will not work as a Content Block. The function I use is as below:

<script runat="server">
    Platform.Load("Core","1.1.3");
    var stream = ContentBlockById("[ContentBlockId]");
    Write(TreatAsContent(stream));     
</script>

Has anybody been able to achieve this?

4
  • have you validated the content block to ensure that the issue is not there (some translation from Classic to Content Builder code can cause errors). Also, are you hosting this inside of an HTML content block, or a code snippet block? Commented Dec 7, 2018 at 19:23
  • Yes, I pasted the content into an email first to make sure it passed validation. I am using an HTML content block with all content being contained within opening and closing AMPscript tags. Commented Dec 7, 2018 at 21:27
  • Try putting in code snippet. I know HTML content areas have wrapping html tags, that may be breaking your script Commented Dec 7, 2018 at 22:37
  • Code Snippet yields the same results, must have been how the call to the block was being made. Commented Dec 11, 2018 at 17:58

1 Answer 1

6

I've accomplished this by using the following SSJS:

<script runat="server">
 var ampscriptCode = Platform.Function.ContentBlockByID("####");
</script>

The AMPscript was held in a regular HTML content block in Content Builder.

Platform Server-side JavaScript functions

4
  • where is the output? You set the var to the content block, but I see nothing outputting the content. Also, to verify, it is recommended by SF for all AMPScript or 'code only' blocks to utilize the 'Code Snippet' block as the HTML block does automatically add in HTML around the code. I am not saying it will not work, but it can cause erratic behavior and/or what may seem to be anomalous errors that are hard to troubleshoot. Commented Dec 10, 2018 at 14:36
  • Any output is done by the AMPscript code. The SSJS above causes the AMPscript to run. This has been running without issue for a long time.
    – jake314159
    Commented Dec 10, 2018 at 15:29
  • Thanks for the elaboration. I am glad this has been running for you without issue - I would only recommend using Code Snippet vs. HTML block if this is the chosen route - to help ensure continued success with this method. Commented Dec 10, 2018 at 18:28
  • This works, thank you! It appears to work for either Code Snippet or regular Content Block (as of the time of this comment) Commented Dec 11, 2018 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .