2

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

@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.

| improve this answer | |
  • Ooh, I like your answer more. Thanks Greg! – Eliot Harper Apr 8 at 1:07
4

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>
| improve this answer | |
  • Grrr, I thought I already tried that, but it works. Thanks! – Eliot Harper Apr 7 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. – Gortonington Apr 7 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.