0

I am using below SSJS to trigger another automation. I don't see any error, it's completing the task successfully but the next automation is not automatically triggering. Can you please look at it and advise what could be the issue.

Thanks.

<script runat="server">

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

var automationCustomerKey = "[xyxyxyx]"

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>
0
1

as a general approach, insert something that tells you where the code ended up interpreting and run your code on a cloudpage, so you can see the result of write statements. See example below with the two Writes.

/*....*/
    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
      Write("1"); 
    }
} else {
   // automation not found
   Write("2");
}

this will help you narrow down the error.

I would hazard a guess that you will end up with condition "1", which tells you your automation doesn't have the right status. If you have never tried this code before, likely your targeted automation isn't set up correctly. Make sure that it has a schedule event, but the automation should not be running. In fact, you don't even need to enter a time. You can be sure your automation is fine if the schedule event on the UI is grey. Also, you should be seeing the option to "Run Once". That is what this code essentially does. Hope this helps.

3
  • Thanks Jonas. So automation B when I manually run (run once) then it run sucessfuly. When I am running automation A where the script is at that time automation B is defined event as schedule but it's not scheduled to any time. So it's UI is greyed out and run once is avaliable. – Sohail Oct 3 '20 at 13:31
  • Well then- run your code with write statements as shown to narrow down what the error is. – Jonas Lamberty Oct 3 '20 at 13:56
  • Thanks Jonas. It works, you were right I was getting value '1'. When I change the value to '2' it works. Thanks again. – Sohail Oct 3 '20 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.