← Back to Snippets
SSJS

Trigger an Automation in SSJS

Trigger an automation to run programmatically. Great for chaining automation runs (where one automation calls another) or for running in cloudpages.


SQL
<script runat="server">
    Platform.Load("core", "1.1.1");

    /**
     * Starts an Automation immediately if it is currently "Ready".
     * @param {string} customerKey - The External Key of the Automation
     * @returns {string} - Status message (e.g., "Started", "Running", "Error")
     */
    function startAutomation(customerKey) {
        
        // 1. Setup the Retrieve Request to find the Automation
        var rr = Platform.Function.CreateObject("RetrieveRequest");
        Platform.Function.SetObjectProperty(rr, "ObjectType", "Automation");
        Platform.Function.AddObjectArrayItem(rr, "Properties", "ProgramID");
        Platform.Function.AddObjectArrayItem(rr, "Properties", "CustomerKey");
        Platform.Function.AddObjectArrayItem(rr, "Properties", "Status");
        
        // 2. Filter by Customer Key
        var sfp = Platform.Function.CreateObject("SimpleFilterPart");
        Platform.Function.SetObjectProperty(sfp, "Property", "CustomerKey");
        Platform.Function.SetObjectProperty(sfp, "SimpleOperator", "equals");
        Platform.Function.AddObjectArrayItem(sfp, "Value", customerKey);
        Platform.Function.SetObjectProperty(rr, "Filter", sfp);
        
        // 3. Execute Retrieve
        var statusOut = [0, 0, 0];
        var results = Platform.Function.InvokeRetrieve(rr, statusOut);

        if (results && results.length > 0) {
            var objectID = results[0]["ObjectID"];
            var currentStatus = results[0]["Status"];

            // Status 2 = Ready
            if (currentStatus == 2) {
                
                // 4. Automation is Ready -> Perform "Start"
                var obj = Platform.Function.CreateObject("Automation");
                Platform.Function.SetObjectProperty(obj, "ObjectID", objectID);
                
                var po = Platform.Function.CreateObject("PerformOptions");
                var performResult = [0, 0, 0];
                var performStatus = Platform.Function.InvokePerform(obj, "start", performResult, po);
                
                return performStatus == "OK" ? "Started" : "Perform Failed: " + performStatus;
                
            } else if (currentStatus == 3) {
                return "Skipped: Automation is already running.";
            } else {
                return "Skipped: Automation status is " + currentStatus + " (Not Ready).";
            }
        } else {
            return "Error: Automation not found.";
        }
    }

    // --- Usage Example ---
    try {
        var result = startAutomation("AUTOMATION_EXTERNAL_KEY");
        Write("Result: " + result);
    } catch (e) {
        Write("Critical Error: " + Stringify(e));
    }
</script>

For a list of status codes and what they mean, check out the official documentation here.