JavaScript Activity to Invoke Another Automation in Marketing Cloud

There are going to be situations where you may want to invoke another automation from one automation. Or you may want to run a series of automations one after another without worrying about setting up a schedule. 

You can use the below mentioned JS code in a script activity in an Automation in Marketing Cloud to invoke another automation. Simply copy / paste this code in a script activity and just make one small change - add the customer key of automation which you want to run/invoke from the current automation. 

When you run this script activity, it will initiate the other automation to run. You can use this activity as last activity in two automations that will invoke each other and can run in an infinite look until you manually pause them. This could be useful in a case where you may want to run an automation earlier than allowed interval of 1 hour. The earliest an automation can run is every 1 hour. You can use this method to trigger an automation earlier than this time and as soon as it is finished. 

You can duplicate the automation which you want to run every 15 or 30 mins and add this JS activity in both of those automation to trigger each other as soon as the one of those automation is completed.

JS Code to be Used in Script Activity in Automation
<script runat="server">

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

var automationCustomerKey = "Enter customer key of the automation you want to invoke"

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

var sfp = Platform.Function.CreateObject("SimpleFilterPart");
Platform.Function.SetObjectProperty(sfp, "Property", "CustomerKey");
Platform.Function.SetObjectProperty(sfp, "SimpleOperator", "equals");
Platform.Function.AddObjectArrayItem(sfp, "Value", automationCustomerKey);

Platform.Function.SetObjectProperty(rr, "Filter", sfp);

var retrieveStatus = [0,0,0];

var automationResultSet = Platform.Function.InvokeRetrieve(rr, retrieveStatus);

var ObjectID = automationResultSet[0]["ObjectID"];
var Status = automationResultSet[0]["Status"];

if (ObjectID != "null") {

    /*
    Code Status
    -1   Error
     0   BuildingError
     1   Building
     2   Ready
     3   Running
     4   Paused
     5   Stopped
     6   Scheduled
     7   Awaiting Trigger
     8   InactiveTrigger
    */

    if (Status == 2) {

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

    } else {
      // already running

    }
} else {
   // automation not found
}

</script>

Comments