Add an Activity to an Automation verified

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

This method works for Script Activities, Query Activities and all the other types of Activities allowed in an Automation.

REST API

var autoObjectId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var payload = {
    "steps": [
        {
            "annotation": "",
            "stepNumber": 0,
            "activities": [
                {
                    "name": "My Script Activity",
                    "objectTypeId": 423,
                    "displayOrder": 2,
                    "activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
                }
            ]
        }
    ]
};

var endpoint = restInstanceUrl + "automation/v1/automations/" + autoObjectId;

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 api = new Script.Util.WSProxy();

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

        var myActivity = {
            "name": "My Script Activity",
            "objectTypeId": 423,
            "displayOrder": 2,
            "activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
        };

        var automationObjectId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

        var endpoint = restInstanceUrl + "automation/v1/automations/" + automationObjectId;

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

        var results = request.send();

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

        var activities = result.steps[result.steps.length - 1].activities;

        activities.push(myActivity);

        var payload = {
            steps: result.steps
        };

        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>
{
    "id": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "legacyId": "AZERTYUIOPQSDFGHJKLM",
    "name": "MyAutomation",
    "description": "",
    "key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "categoryId": 3267,
    "statusId": 2,
    "lastSavedDate": "2022-12-13T11:16:15.377",
    "lastSavedByName": "Some app user",
    "createdDate": "2022-12-13T10:16:59.39",
    "createdByName": "",
    "updateInProgress": false,
    "steps": [
        {
            "annotation": "",
            "stepNumber": 0,
            "activities": [
                {
                    "id": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                    "name": "My Script Activity",
                    "description": "",
                    "activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                    "objectTypeId": 423,
                    "displayOrder": 2,
                    "serializedObject": ""
                }
            ]
        }
    ],
    "startSource": {
        "typeId": 1,
        "schedule": {
            "scheduleTypeId": 3,
            "startDate": "0001-01-01T00:00:00",
            "endDate": "0001-01-01T00:00:00",
            "rangeTypeId": 0,
            "occurrences": 0,
            "icalRecur": "FREQ=DAILY;INTERVAL=1",
            "timezoneId": 79,
            "statusId": 0
        }
    }
}

WARNING

In order to add a new activity, please first retrieve the exisiting ones and add them to the payload. Otherwise, the existing activities will be overwritten by the PATCH call.

ObjectTypeId property reference

IdAutomation Activity
42User-initiated Email
43Import Definition
45Group Definition
53File Transfer Activity
73Data Extract Activity
84Report Activity
300Query Activity
303Filter Activity
423Server Side Javascript Activity
425ELT Activity
427Build Audience Activity
467Program Wait
724Mobile Automation List Refresh Instance
725MobileConnect Message Instance
726Mobile File Import Instance
733InteractionStudio
736MobilePush Message Object Instance
749Interaction Studio Event
756Interaction Studio Date Event
771Salesforce Send Activiy
783GroupConnect
1010Thunderhead Transfer Activity
1101Interaction Studio Decision Activity
1701PredictiveIntelligenceRecommendationActivity

Reference

Ressources and references related to the current methods.

SOAP object

Last Updated: