3

Is it possible to create SSJS functions as code resources in Web Studio and then use those functions in other CloudPages or Script Activities?

For example, if I created a function that generates an access token, I can definitely use it in the same CloudPage/script activity that it's created, but this might be useful in other places as well within the same Marketing Cloud account. I would ideally create this function as a code resource and then reference it in all activities that need it.

<script runat="server">  

Platform.Load("Core","1");

function generateToken(clientId, clientSecret, authURL) {
  var authUrl = authURL;
  var contentType = 'application/json';
  var authJSON = {
    "clientId": clientId,
    "clientSecret": clientSecret
  }
  var authPayload = Platform.Function.Stringify(authJSON);

  var accessTokenResult = HTTP.Post(authUrl, contentType, authPayload);

  var statusCode = accessTokenResult["StatusCode"];
  var response = accessTokenResult["Response"][0];
  var accessToken = Platform.Function.ParseJSON(response).accessToken;

  return accessToken;

};

</script>

Thanks!

1 Answer 1

2

You kind of answered your own question. You would save it as a code snippet in Content Builder and then reference that snippet in any Cloudpage/email/etc. that you would use it in. This will then add the referenced function to your code.

For example:

Your Content Builder Code Snippet:
Content Block Key: TokenFunction

<script runat=server>
Platform.Load("core", "1.1.1");

function generateToken(clientId, clientSecret, authURL) {
  var authUrl = authURL;
  var contentType = 'application/json';
  var authJSON = {
    "clientId": clientId,
    "clientSecret": clientSecret
  }
  var authPayload = Platform.Function.Stringify(authJSON);

  var accessTokenResult = HTTP.Post(authUrl, contentType, authPayload);

  var statusCode = accessTokenResult["StatusCode"];
  var response = accessTokenResult["Response"][0];
  var accessToken = Platform.Function.ParseJSON(response).accessToken;

  return accessToken;

}
</script>

Then you reference it inside your Cloudpage:

Platform.Function.ContentBlockByKey("TokenFunction");

So put it all together and you get something like:

<script runat=server>
Platform.Load("core", "1.1.1");

Platform.Function.ContentBlockByKey("TokenFunction")

var Id = "{{yourID}}"
var Secret = "{{yourSecret}}"
var URL = "https://auth.exacttargetapis.com/v1/requestToken"

var accessToken = generateToken(Id, Secret, URL)

Write("token: " + accessToken);


</script>

Which will output token: XXXXXXX where the X's represent your token characters.

My recommendation would be to put together more than a single function per code snippet and store it as a 'library' rather than individual functions.

1
  • Thank you Gortonington, this helps immensely. My missing piece was the ContentBlockByKey. Your recommendation about creating a library is exactly my use case! Trying to handle and refresh the access token when using the APIs from within Marketing Cloud.
    – RobTheBank
    Commented Feb 27, 2019 at 23:42

You must log in to answer this question.

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