Create a Script Activity verified

Learn how to create a Script Activity in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the REST API method.

REST API

var script =  '<script runat="server">Platform.Load("core", "1");Write("Hello World");<script>';

var payload = {
   "name": "MyScript",
   "key": GUID(),
   "description": "",
   "script": script,
   "categoryId": 12345
}

var endpoint = restInstanceUrl + "automation/v1/scripts";

var request = new Script.Util.HttpRequest(endpoint);
    request.emptyContentHandling = 0;
    request.retries = 2;
    request.continueOnError = true;
    request.setHeader("Authorization", "Bearer " + accessToken);
    request.method = "POST";
    request.contentType = "application/json";
    request.encoding = "UTF-8";
    request.postData = payload;

var results = request.send();

var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">

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

    var api = new Script.Util.WSProxy();

    var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
        accessToken     = "YOUR_REST_API_TOKEN";
	
	try {

        var request = api.retrieve("DataFolder", ["ID"], {
            LeftOperand: {
                Property: "Name", 
                SimpleOperator: "equals",
                Value: "Scripts" 
            },
            LogicalOperator: "AND",
            RightOperand: {
                Property: "ContentType", 
                SimpleOperator: "equals", 
                Value: "ssjsactivity" 
            }
        });

        var categoryId = request.Results[0].ID;

        var script =  '<script runat="server">Platform.Load("core", "1");Write("Hello World");<script>';

        script = script.replace(/<script[\s\S]*?>/gi, '');
        script = script.replace(/<\/script>/gi, '');    

        var payload = {
            "name": "MyScript",
            "key": GUID(),
            "description": "",
            "script": script,
            "categoryId": categoryId
        }

        var endpoint = restInstanceUrl + "automation/v1/scripts";

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "POST";
            request.contentType = "application/json";
            request.encoding = "UTF-8";
            request.postData = Stringify(payload);

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

        Write(Stringify(result));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
{
  "ssjsActivityId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
  "name": "MyScript",
  "key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
  "description": "",
  "script": "Platform.Load(\"core\", \"1\");Write(\"Hello World\");",
  "categoryId": 3130,
  "statusId": 1,
  "status": "Active",
  "createdDate": "2022-10-11T06:02:01.483",
  "modifiedDate": "2022-10-11T06:02:01.483",
  "createdBy": 715074318,
  "modifiedBy": 715074318,
  "folderLocationText": "Scripts",
  "parentCategoryId": [
    
  ]
}

Last Updated: