Update a Script Activity verified

Learn how to update 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 there");<script>';

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

var payload = {
    "ssjsActivityId": id,
    "description": "This script has been updated",
    "script": script
}

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

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

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";

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

        var queryString =   "$fields=objectId";
            queryString +=  "&$filter=" + filter;
            queryString +=  "&$pagesize=1";
            queryString +=  "&$page=1";

        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));

        var id = result.items[0].ssjsActivityId;

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

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

        var payload = {
            "ssjsActivityId": id,
            "description": "This script has been updated",
            "script": script
        }

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

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "PATCH";
            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": "This script has been updated",
    "script": "Platform.Load(\"core\", \"1\");Write(\"Hello there\");",
    "categoryId": 0,
    "statusId": 1,
    "status": "Active",
    "createdDate": "0001-01-01T00:00:00",
    "modifiedDate": "0001-01-01T00:00:00",
    "createdBy": 12345678,
    "modifiedBy": 12345678
}

Last Updated: