[SalesForce] Accessing SSJS variables and functions from a Content Block

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?

Best Answer

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

Related Topic