Start or stop an Automation verified

Learn how to start or stop an Automation in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include Platform and WSProxy methods.

Platform

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

var automation = Platform.Function.CreateObject("Automation");

Platform.Function.SetObjectProperty(automation, "ObjectID", objectID);

var options = Platform.Function.CreateObject("PerformOptions");

var result = [0, 0, 0];

var status = Platform.Function.InvokePerform(automation, "start", result, options);
<script runat="server">

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

	try {

        var name = "MyAutomation";

        var request = Platform.Function.CreateObject("RetrieveRequest");

        Platform.Function.AddObjectArrayItem(request, "Properties", "Status");
        Platform.Function.AddObjectArrayItem(request, "Properties", "ProgramID");

        Platform.Function.SetObjectProperty(request, "ObjectType", "Automation");

        var filter =  Platform.Function.CreateObject("SimpleFilterPart");

        Platform.Function.SetObjectProperty(filter, "Property", "Name");
        Platform.Function.SetObjectProperty(filter, "SimpleOperator", "equals");
        Platform.Function.AddObjectArrayItem(filter, "Value", name);

        Platform.Function.SetObjectProperty(request, "Filter", filter);

        var result = Platform.Function.InvokeRetrieve(request, [0, 0, 0]);

        var objectID = result[0]["ObjectID"];

        var automation = Platform.Function.CreateObject("Automation");

        Platform.Function.SetObjectProperty(automation, "ObjectID", objectID);

        var options = Platform.Function.CreateObject("PerformOptions");

        var result = [0, 0, 0];

        var status = Platform.Function.InvokePerform(automation, "start", result, options);

        Platform.Response.Write(Platform.Function.Stringify(result));
		
	} catch(error) {

        Platform.Response.Write(Platform.Function.Stringify(error));

    }	

</script>
[
    "Performed Activity",
    0,
    "{\"Object\":{\"Schedule\":null,\"AutomationTasks\":null,\"IsActive\":true,\"IsActiveSpecified\":true,\"AutomationSource\":null,\"Status\":0,\"StatusSpecified\":false,\"Notifications\":null,\"ScheduledTime\":\"0001-01-01T00:00:00.000\",\"ScheduledTimeSpecified\":false,\"AutomationType\":null,\"UpdateModified\":false,\"UpdateModifiedSpecified\":false,\"LastRunInstanceID\":null,\"CreatedBy\":0,\"CreatedBySpecified\":false,\"CategoryID\":null,\"LastRunTime\":\"0001-01-01T00:00:00.000\",\"LastRunTimeSpecified\":false,\"LastSaveDate\":\"0001-01-01T00:00:00.000\",\"LastSaveDateSpecified\":false,\"ModifiedBy\":0,\"ModifiedBySpecified\":false,\"RecurrenceID\":null,\"LastSavedBy\":0,\"LastSavedBySpecified\":false,\"InteractionObjectID\":null,\"Name\":\"\",\"Description\":\"\",\"Keyword\":null,\"Client\":null,\"PartnerKey\":null,\"PartnerProperties\":,\"CreatedDate\":\"0001-01-01T00:00:00.000\",\"CreatedDateSpecified\":false,\"ModifiedDate\":null,\"ModifiedDateSpecified\":false,\"ID\":0,\"IDSpecified\":false,\"ObjectID\":\"S0M3-GU1D-K3Y-G03SR1G4T-H3R3\",\"CustomerKey\":\"S0M3-GU1D-K3Y-G03SR1G4T-H3R3\",\"Owner\":null,\"CorrelationID\":null,\"ObjectState\":null,\"IsPlatformObject\":false,\"IsPlatformObjectSpecified\":false},\"Task\":null,\"ProgramActivityInstanceId\":null,\"StatusCode\":\"OK\",\"StatusMessage\":\"Performed Activity\",\"OrdinalID\":0,\"OrdinalIDSpecified\":false,\"ErrorCode\":0,\"ErrorCodeSpecified\":false,\"RequestID\":null,\"ConversationID\":null,\"OverallStatusCode\":null,\"RequestType\":\"Synchronous\",\"RequestTypeSpecified\":false,\"ResultType\":null,\"ResultDetailXML\":null}"
]

WSProxy

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

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

var props = {
    ObjectID: objectID
};

var action = ["start", "stop"];

var opts = {};

var result = api.performItem("Automation", props, action[1], opts);
<script runat="server">

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

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

    try {

        var name = "MyAutomation";

        var cols = [
            "Name",
            "ProgramID",
            "CustomerKey",
            "Status"
        ];

        var request = api.retrieve("Automation", cols, {
            Property: "Name",
            SimpleOperator: "equals",
            Value: name
        });

        var objectID = request.Results[0].ObjectID;

        var props = {
            ObjectID: objectID
        };

        var action = ["start", "stop"];

        var opts = {};

        var result = api.performItem("Automation", props, action[1], opts);

        Write(Stringify(result));

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

</script>
{
    "Status": "OK",
    "StatusMessage": "",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "Object": {
                "Schedule": null,
                "AutomationTasks": null,
                "IsActive": true,
                "AutomationSource": null,
                "Status": 0,
                "Notifications": null,
                "ScheduledTime": "0001-01-01T00:00:00.000",
                "AutomationType": null,
                "UpdateModified": false,
                "LastRunInstanceID": null,
                "CreatedBy": 0,
                "CategoryID": null,
                "LastRunTime": "0001-01-01T00:00:00.000",
                "LastSaveDate": "0001-01-01T00:00:00.000",
                "ModifiedBy": 0,
                "RecurrenceID": null,
                "LastSavedBy": 0,
                "InteractionObjectID": null,
                "Name": "",
                "Description": "",
                "Keyword": null,
                "Client": null,
                "PartnerKey": null,
                "PartnerProperties": [],
                "CreatedDate": "0001-01-01T00:00:00.000",
                "ModifiedDate": null,
                "ID": 0,
                "ObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Owner": null,
                "CorrelationID": null,
                "ObjectState": null,
                "IsPlatformObject": false
            },
            "Task": null,
            "ProgramActivityInstanceId": null,
            "StatusCode": "OK",
            "StatusMessage": null,
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

Reference

Ressources and references related to the current methods.

Official documentation
Create trigger
SOAP object

Help me turn coffee into code

This website is provided to you free of charge. However, a lot of time and effort are spent to write, test and mainain the code. Please consider supporting my work by buying me a cup of coffee.

Last Updated: