Retrieve Script Activity verified

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

REST API

var name = "MyScript",
    pagesize = 300,
    page = 1,
    fields = [
        "ssjsActivityId",
        "name",
        "key",
        "description",
        "script",
        "categoryId",
        "createdDate",
        "modifiedDate"
    ];

var filter = "name%20eq%20'" + name + "'";

var queryString =   "$fields=" + fields.join(",");
    queryString +=  "&$filter=" + filter;
    queryString +=  "&$pagesize=" + pagesize;
    queryString +=  "&$page=" + page;

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

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

var results = request.send();

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

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

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

	try {

        var name = "MyScript",
            pagesize = 300,
            page = 1,
            fields = [
                "ssjsActivityId",
                "name",
                "key",
                "description",
                "script",
                "categoryId",
                "createdDate",
                "modifiedDate"
            ];

        var filter = "name%20eq%20'" + name + "'";

        var queryString =   "$fields=" + fields.join(",");
            queryString +=  "&$filter=" + filter;
            queryString +=  "&$pagesize=" + pagesize;
            queryString +=  "&$page=" + page;

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

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

        var results = request.send();

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

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

</script>
{
  "count": 1,
  "page": 1,
  "pageSize": 300,
  "items": [
    {
      "ssjsActivityId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
      "name": "MyScript",
      "key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
      "description": "This script has been updated",
      "script": "Platform.Load(\"core\", \"1\");Write(\"Hello World\");",
      "categoryId": 0,
      "createdDate": "2022-10-11T06:02:01.483",
      "modifiedDate": "2022-10-11T06:39:46.78"
    }
  ]
}

Last Updated: