Script Activity – The underrated SFMC powerhouse

Script Activity – The underrated SFMC powerhouse

Perhaps you haven’t heard about Automation Studio’s script activity yet? Or maybe you’re still wondering what you can do with it. You’re not alone, as this powerful activity type’s sorely underused. However, you’ll appreciate its flexibility and power once you see what it can do. Many MVPs, Marketing Champions, and passionate power users swear by these activities and utilize them for complex situations otherwise requiring external software solutions.

The basics

You can find script activity in the “Activities” section of Automation Studio and add it to the automation workflow just like any other activity.

As it can be utilized to run Server-Side JavaScript (SSJS) code, it bears all the capabilities of SSJS without the hassle of worrying about long page load times and how to schedule tasks. Furthermore, using a script activity avoids anyone guessing the CloudPage URL, as it’s not accessible publicly.

While it is a “Script Activity” and should therefore be clear to Marketing Cloud that it contains SSJS code, you must use a script-tag within the activity configuration just as you would on a CloudPage to indicate where the SSJS code is located:

				
					<script runat="server">
   /* Your code goes here */
</script>


				
			

But what exactly can I do with it?

Here are some examples of how you can use script activity:

  • Interact with the Marketing Cloud API or SSJS Marketing Cloud functions to:
    • Trigger other automations or activities within an automation. Run them on demand or as soon as a task is finished rather than the typical schedule.
    • Dynamically create data extensions based on data fetched using your script.
    • Initiate the contact deletion process.
  • Interact with external APIs:
    • Fetch data from a blog via API and write it to a data extension to send a fully automated newsletter.
    • Perform batch updates in Sales and Service Cloud using the Salesforce Bulk API to optimize for performance and avoid mass updates from Journeys or CloudPages.
    • Connect external services (print providers, snail mail providers, custom push or SMS, etc.).
    • Fetch key metrics or data required for personalization using an API to avoid heavy API use within emails. Avoid using integration middleware for minor data points (e.g., number of current auctions at an auction platform, the number of job offers for a career page, etc.)

What if something goes wrong?

As with your Marketing Cloud Server-Side JavaScript code, you must take care to avoid errors. 

First, every time an error is thrown, the automation will fail; thus, the activity will show as failed. No further steps will run automatically. Unfortunately, the error message isn’t displayed anywhere in Automation Studio. That’s where you must be prepared to step up your debugging game: Create a log for your error messages.

Create a data extension as an error log and catch any errors using try-catch, logging the message, and then throwing the error again. Hence, the automation still fails if an error occurs (this step is essential because otherwise, the activity will be seen as successful by the system).

Here’s what such an error logging code would look like:

				
					<script runat="server">
  Platform.Load("Core", "1");
  try {
      /* Your code goes here */
  } catch (e) {
      logError(e);
      throw e;
  }
  // Function for error logging
  function logError(e) {
      // Initiate data extension used for error logging
      var errorLogDEName = "ScriptActivity_ErrorLog";
      var errorLog = DataExtension.Init(errorLogDEName);
      // Truncate data, remove disturbing line breaks, etc.
      var message = e.message ? e.message.substring(0, 2000) : "";
      var errorTrace = Stringify(e).replace(/[\n\r]|\\n|\\r/g, '').substring(0, 4000);
      // Write error to the data extension
      errorLog.Rows.Add({
          "scriptName": "myTestScript",
          "id": Platform.Function.GUID(),
          "message": message,
          "error": errorTrace
      });
  }
</script>

				
			

You can also add custom error codes if you’re writing longer scripts with custom checks that throw errors for invalid input data, etc. That’s the beauty of SSJS activities; you’re free to extend and customize to fit your needs!

Conclusion

The great flexibility that comes with Script Activities (and Custom Activities for Journey Builder, for that matter) might just be what sets Salesforce Marketing Cloud apart from its competition. So if you have a requirement that seems unsolvable, think again and consider using a script activity.

We hope this sparks enough interest for you to embark on your SSJS journey. And if you’re not that experienced with JavaScript, perhaps this will motivate you to start exploring and learning.

Picture of Markus Dang

Markus Dang

Markus is a guest author at DESelect. He's a Marketing Cloud Unit Lead & Solution Architect. He is also a Salesforce Marketing Cloud Champion.

Stay Connected

Stay Connected

Reach the most targeted<br/> audiences in half the time

Reach the most targeted
audiences in half the time

Deselect icon
Subscribe to the Newsletter
Receive Salesforce Marketing Cloud tips,
tricks, and DESelect updates

By clicking the “Subscribe now” button, you agree to the DESelect Terms of Use and Privacy Policy.