6

Let me preface this by saying that I am not looking at cloud pages. I am looking to either use Content Blocks or other Activity Scripts to store JS functions which I can load into an Automation Script Activity when needed; just as if I was using Require in JS to load another file. Prior to creating this question; I reviewed two different posts on similar use cases but not quite the same.

Sample Script Activity (Automation Studio)

<script runat="server" language="javascript">
  Platform.Load("Core", "1.1.1");

  var environment = "test";

  var contentBlockKey = environment + "_" + "GetFirstName" + ".js";
  Platform.Function.ContentBlockByKey(contentBlockKey);

  var contentBlockKey = environment + "_" + "GetLastName" + ".js";
  Platform.Function.ContentBlockByKey(contentBlockKey);

  var contentBlockKey = environment + "_" + "GetFullName" + ".js";
  Platform.Function.ContentBlockByKey(contentBlockKey);
  
  var de = DataExtension.Init("MyLog");
  var result = fullname("john", "doe");
  de.Rows.Add({message: result});
</script>

Sample Code Snippet (Email Studio)

  • Name: test_GetFirstName.js
  • Customer Key: test_GetFirstName.js
<script runat="server" language="javascript">
  Platform.Load("Core", "1.1.1");

  function firstname(name) {
    /* do stuff */
    return name;
  }
</script>

Sample Code Snippet (Email Studio)

  • Name: test_GetLastName.js
  • Customer Key: test_GetLastName.js
<script runat="server" language="javascript">
  Platform.Load("Core", "1.1.1");

  function lastname(name) {
    /* do stuff */
    return name;
  }
</script>

Sample Code Snippet (Email Studio)

  • Name: test_GetFullName.js
  • Customer Key: test_GetFullName.js
<script runat="server" language="javascript">
  Platform.Load("Core", "1.1.1");

  function fullname(a, b) {
    /* do stuff */
    return firstname(a) + " " + lastname(b);
  }
</script>

The Problem: This way of trying to share functions so that they can be used across different Script Activities doesn't work. Even if I move the ContentBlockByKey calls to the top of the Script Activity with their own <script runat="server"></script> tags, it still doesn't work. In particular I can't carry the environment variable between script tags/blocks.

I've written universal/sharable functions such as:

  • GetDataExtensionExternalKeyByName(name)
  • GetAutomationExternalKeyByName(name)
  • GetAutomationHistory(key)

However, I am NOT able to figure out how to share them. Ideally, I want them in:

  1. Grouped in Code Snippets (Email Studio) and/or separate Code Snippets depending on the use case. For example; All automation-related functions would be in one code snippet, while all data extension related functions would be in another.
  2. Alternatively; I would love to just be able to store the code in different script activities and use Require or something to load them.

Any feedback or guidance is appreciated. Thank you.

Additional Issue; WSProxy declaration must be in the parent script and cannot be declared and initialized in a function.

2
  • 1
    Do you know of he SSJS Lib? You could probably look how it's structured and imported and adapt it the loading method to your needs. You will find it here: github.com/email360/ssjs-lib May 27, 2022 at 7:44
  • Thanks for the suggestion but I can't use Cloud Pages; they aren't accessible to me.
    – JordanGS
    May 27, 2022 at 7:51

0

You must log in to answer this question.

Browse other questions tagged .