5

I'm trying to figure out a way to inject SSJS code in a CloudPage instead of having to copy and paste each time. That is, I have a library of functions that I want to manage centrally and include in different pages.

I know I can publish the code as a Code Resource page in CloudPages and use HTTPGet and TreatAsContent functions to access it from a landing page, but I'm wondering if it's possible to store the content as a content block and access variables or functions within it.

For example, I have a Code Snippet content block that looks like this:

<script runat=server>
function multiply(p1, p2) {
  return p1 * p2; 
}
</script>

And my CloudPage references the content block:

<script runat=server>
Platform.Load('core','1');
try {

Platform.Function.TreatAsContent(Platform.Function.ContentBlockByID(226637));

var calc = multiply(2,4);

Platform.Response.Write('result: ' + calc);
} catch (ex) {
    Write("error message: " + ex);
}

</script>

However the script can't find the function, even though I'm injecting it on the page. I get the error:

error message: Object expected: multiply

AMPscript lets you set variables in a Content Block and then use them. For example this content block:

%%[

var @msg
set @msg = 'hello there'

]%%

...is used on the page:

%%[

TreatAsContent(ContentBlockByID(226637))
OutputLine(Concat('message: ', @msg))

]%%

and outputs:

message: hello there

Is there a way of doing something similar in SSJS?

2 Answers 2

5

@NickHoldren's answer is a great one! I am very intrigued by the possibilities there. I am not looking to subtract from that answer, but to instead provide an alternative solution that will allow you to retain existing structure of functions to be utilized via a content block.

Utilizing the same functions he has in his example, you would build as follows:

Content Block - Code Snippet

<script runat=server>
  function multiply (p1, p2) {
      return p1 * p2; 
  }
</script>


Code in Email/Cloud Page

<script runat=server>
  Platform.Function.ContentBlockByID(112233);
</script>
<script runat=server>
  Platform.Load("Core", "1.1.1");
  var result = multiply(2,200);
  Write('result);
</script>

Which will output 400.

To note a couple things.

  1. Inside the block you need an open and close script tag.
  2. You will need to call the ContentBlockByID in its own script section, and this will need to be above all code that would utilize the functions.

SSJS works in a cascading fashion, similar to AMPscript, but it processes each block individually as well. So it will completely run block 1 before moving to block 2. (This does not include Lookups, Inserts, etc. as they are handled differently). So by placing it in a block above it, this will then render that content to be available for future blocks to utilize.

As another note, its fairly useless, but you can kinda combine them both and remove the need to open/close blocks manually. You can use an eval on the Content block, but have it use the script open before and script close after. Like so:

eval('<script runat=server>' + Platform.Function.ContentBlockByID(105869) + '<' + '/script>');

This will also run it all utilizing the same content block as I showed above.

4
  • Ooh, I like your answer more. Thanks Greg! Commented Apr 8, 2020 at 1:07
  • This is an awesome answer, I just spent hours trying to hack this together myself when I should have come here first. Gortonington to the rescue! Just a side note for anyone else that within the Content Block you can place this code to retrieve the library from an external source. %%=TreatAsContent(HttpGet("[URL]"))=%%. Using this code directly instead of the ContentBlockByID in the answer above doesn't work. The CloudPage needs to go through the ContentBlock first. Commented Sep 7, 2022 at 14:20
  • Can this work for variables as well? Seems like this approach will work for functions, however if you had a Code Snippet: <script runat=server>var myVariable = "Hello World"</script> and wanted to pull this in, it doesnt work the same as functions.
    – charliefay
    Commented Mar 4, 2023 at 4:35
  • @charliefay - To utilize variables set inside of a Content Block in SSJS, you need to create a global variable in your script prior to calling in the content block. So for example: <script runat=server>var myGlobVar; {{call of contentblock defining myGlobVar}} if (myGlobVar == '1') { .... } Commented Mar 6, 2023 at 17:38
6

Assume you have your SSJS written as an object like so inside a Content Block:

var SFMC = {
    multiply: function (p1, p2) {
      return p1 * p2; 
  }
}

You can pull it into a CloudPage or anywhere else by using:

<script runat = "server" type="text/javascript">
  Platform.Load("Core", "1");
  var SFMC = eval(Platform.Function.ContentBlockByID('YOUR_CONTENT_BLOCK_ID'));
</script>

Then you can make use of the function like so:

<script runat = "server" type="text/javascript">
  Platform.Load("Core", "1");
  var SFMC = eval(Platform.Function.ContentBlockByID('YOUR_CONTENT_BLOCK_ID'));
  var result = SFMC.multiply(1,2);
</script>
2
  • Grrr, I thought I already tried that, but it works. Thanks! Commented Apr 7, 2020 at 6:38
  • 1
    @EliotHarper - It also works if you have it in its own script tag at the top (open, call content, close) this will then compile the content and allow it to be utilized in future script blocks. Commented Apr 7, 2020 at 12:33

You must log in to answer this question.

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