1

Here's my code, very stripped down to show the issue:

<script type="javascript" runat="server">
    Platform.Load("Core", "1.1.1");
  try {
    Platform.Function.TreatAsContent(Platform.Function.ContentBlockByName("a_shared_content_block"));

    var Utils = new ScriptUtils();
  }
  catch(e) {
   Write(Stringify(e));
  }
</script>

When I run this inside a CloudPage, here's the error I get:

{"message":"Unknown type: ScriptUtils","description":"Jint.JintException: Unknown type: ScriptUtils - from Jint\r\n\r\n"}

Clearly, the issue is that the call out to the Shared Content block is not rendering itself for use within the script - because ScriptUtils is defined in said Content Block. Any ideas what I might be doing wrong?

1 Answer 1

3

EDIT

So I investigated this a bit more and it appears you can still use it inside the same block, but you need to have the open and close script tags inside of the content. See below example:

<script runat=server>
  Platform.Load("Core","1.1.1");
  
  Platform.Function.ContentBlockByKey("writeDebug")

  var label = "MyLabel"
  var output = "This is my String"
  
  writeDebug(label,output)
</script>

Which pulls from this contentblock:

<script runat=server>
  Platform.Load("Core","1.1.1");
  
  function writeDebug(label,output) { 
    Write('<b>' + label + '</b>: ' + output + '<br>') 
  }
</script>

and outputs:

<b>MyLabel</b>: This is my String<br>

As a note, I have found this is not always true - so if you get an error on this method (first validate that you have open and close script tags in content block) then you can move this to its own block which should resolve the issue.


You need to have the content block run in a separate script block for it to actually run the scripts and be available to the rest of your script. Like this:
<script type="javascript" runat="server">
    Platform.Load("Core", "1.1.1");
 Platform.Function.TreatAsContent(Platform.Function.ContentBlockByName("a_shared_content_block"));
</script>

<script type="javascript" runat="server">
    var Utils = new ScriptUtils();
</script>

That should get you what you want. I removed your try/catch, but you can add it back in if you want it there. As to why it is this way, I do not have a great answer, just a guess that the script without the contentblock stuff is validated first which means it does not see the funtion for ScriptUtils and tosses an error.

You must log in to answer this question.

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