0

I am trying to retrieve status of automation instance object to see status of the last 4(in last 24 hours- since the automation runs in every 6 hours) instances of the automation. I am using the below code, but it retrieves only 3 records which are not even the last three. Not sure what is going wrong!

<script runat="server">
  Platform.Load("core","1");
    var prox = new Script.Util.WSProxy();
 var BU = [{"marketCode": "Belgium", "mid":mid}]
    
   for (i = 0; i < BU.length; i++) {
       var mid = BU[i].mid;
      prox.setClientId({"ID": mid});
   }
var AutomationInstance = "AutomationInstance";
 
     var childcols = ["CustomerKey","Status","StartTime","StatusLastUpdate"];  
                    var childfilter = {
          
                    Property:"CustomerKey",
                    SimpleOperator:"EQUALS",
                    Value: CustomerKey
           };
                 var childreqID = null;
                     var childprops = null;
                  var childmoreObjs = true;
        // var childobjs = prox.retrieve(AutomationInstance, childcols, childfilter);
               
        try
{
          while (childmoreObjs) 
{
          childmoreObjs = false;
                      var childobjs = childreqID == null ? prox.retrieve(AutomationInstance, childcols, childfilter) : prox.getNextBatch(childobjs, childreqID);
          if (childobjs != null) {
            childmoreObjs = childobjs.HasMoreRows;
                childreqID = childobjs.RequestID;
            
              for (var z = 0; z <childobjs.length; z++) {
              
                var automationInsitanceObj = childobjs.Results[z];
                Platform.Response.Write("Start Time: "+automationInsitanceObj.StartTime+ " ");
                 Platform.Response.Write("Customer Key: "+automationInsitanceObj.CustomerKey);
                
              }// end for 
          }//end if
                  }// end while
                }// end try
               
  catch (e) {
      Platform.Response.Write("min for" + Stringify(e)); 
  }
</script>
1

In your code there is a mistake in for loop . You are doing:

for (var z = 0; z < childobjs.length; z++)

You should check the results length:

for (var z = 0; z < childobjs.Results.length; z++)
0

Something I have run into that sounds similar to your experience is that retrieving automations (or automationInstances) by CustomerKey can be confusing. MAYBE this helps, at least it should help debugging. unfortunately too long for a comment:

Are you sure you are looking at the right automation?

A lookup / retrieval of an automation by CustomerKey will potentially return multiple results. CustomerKey is not necessarily unique in case you've deleted an automation of the same CustomerKey and used it again for the next iteration. This happens if you have defined a specific key for your automation and kept using it.

In this case, the "ObjectID" helps as it is a truly unique identifier. You can also leverage the "Status" field which will indicate what state the automation is in. So before running any lookups on your Automation, ensure it is the correct one by retrieving all of the automations with this CustomerKey, outputting both the ObjectID for further use, and the Status to better identify the relevant one. See here: How can I obtain the status of an automation in a Business Unit using AMPscript?

1
  • Thank you for your comment! However, I am not looking for status of the automation. Because my automation involves some queries, which sometime time out and automation instance is error. However, the status of the automation will still show scheduled as per the code in the link you mentioned. Makes sense? – Swati Mishra Jul 15 '20 at 12:48

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.