0

I'm building an automation to retrieve the latest statuses for a number of specified automation instances. The flow is something like run SJSS to retrieve all statuses for the automation instance and write them to a data extension -> run SQL query activity to select the latest status in the data extension. I had this working ok then realised that when in production it wouldn't give correct answers due to the 2500 record limit of the retrieve method from the SOAP API. I decided to use the example from the Marketing Cloud docs to do an advance retrieve that would make the requests in batches https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_WSProxy_advanced_retrieve.html. Given that my code was getting more complicated I decided to start testing it in a Cloud Page SJSS code resource.

Here is my code:

<script runat="server">
Platform.Load("core", "1.1.1");
var prox = new Script.Util.WSProxy();
var moreData = true
var reqID = null
var cols = ["Name","Status","StatusLastUpdate","StatusMessage"];
var filter = {
    Property: "Name",
    SimpleOperator: "equals",
    Value: "ScholarOne_Ingest"
};

while(moreData) {
    Write('Entered while block\n\n');
    moreData = false;
    var data = reqID == null ?
           prox.retrieve("AutomationInstance", cols, filter) :
           prox.getNextBatch("AutomationInstance", reqID);
  
    Write('Data.HasMoreRows: '+ data.HasMoreRows + '\n\n');
    Write('Data.RequestID: '+ data.RequestID + '\n\n');
    Write('Results Length: '+ data.Results.length + '\n\n');

    if(data != null) {
        moreData = data.HasMoreRows;
        reqID = data.RequestID;
        if(data && data.Results) {
            for (var i = 0; i < data.Results.length; i++) {
              var autoName = data.Results[i].Name;
              var autoStatus = data.Results[i].Status;
              var autoStatusLastUpdate = data.Results[i].StatusLastUpdate;
              var autoStatusMessage = data.Results[i].StatusMessage;

              var data = {
                AutomationName: autoName,
                AutomationStatus: autoStatus,
                AutomationLastUpdate: autoStatusLastUpdate,
                AutomationStatusMessage : autoStatusMessage
              }
              //Write(de.Rows.Add(data));
              Write('AutomationName: ' + data["AutomationName"] + '\n\n');
              Write('AutomationStatus: ' + data["AutomationStatus"] + '\n\n');
              Write('AutomationLastUpdate: ' + data["AutomationLastUpdate"]  + '\n\n');
              Write('AutomationStatusMessage: ' + data["AutomationStatusMessage"]  + '\n\n');
              Write('\n\n');    
           }

        }
    }
}

Here is the output when following the URL for the code resource Entered while block

Data.HasMoreRows: false

Data.RequestID: 1d26ecbd-fa02-40e6-a30a-64cf0025b669

Results Length: 9

i: 0

AutomationName: ScholarOne_Ingest

AutomationStatus: 1

AutomationLastUpdate: Thu, 04 Nov 2021 04:02:15 GMT-06:00

AutomationStatusMessage: Complete

I can't figure out why it doesn't seem to be iterating through the for loop. By comparison when I was just doing a simple retrieve without using batches the for loop was working fine and multiple rows were being written to a data extension.

e.g.

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

var prox = new Script.Util.WSProxy();
var cols = ["Name","Status","StatusLastUpdate","StatusMessage"];
var filter = {
    Property: "Name",
    SimpleOperator: "equals",
    Value: "ScholarOne_Ingest"
};

//get automation instance properties
var response = prox.retrieve("AutomationInstance", cols, filter);

var de = DataExtension.Init("CB539ECD-FFAC-4A3C-8556-51E0A09D0B22");

for (var i = 0; i < response.Results.length; i++) {
        var autoName = response.Results[i].Name;
        var autoStatus = response.Results[i].Status;
        var autoStatusLastUpdate = response.Results[i].StatusLastUpdate;
        var autoStatusMessage = response.Results[i].StatusMessage;

        var data = {
            AutomationName: autoName,
            AutomationStatus: autoStatus,
            AutomationLastUpdate: autoStatusLastUpdate,
            AutomationStatusMessage : autoStatusMessage
        }
        Write(de.Rows.Add(data));
          
 }

</script>

Why isn't the for loop being iterated through with each result being written out?

1 Answer 1

1

You're resetting the data var inside your loop. I changed the second instance to data2:

<script runat="server">
Platform.Load("core", "1.1.1");
var prox = new Script.Util.WSProxy();
var moreData = true
var reqID = null
var cols = ["Name","Status","StatusLastUpdate","StatusMessage"];
var filter = {
    Property: "Name",
    SimpleOperator: "equals",
    Value: "DataExtension1 Import"
};

while (moreData) {

    moreData = false;
    var data = reqID == null ? prox.retrieve("AutomationInstance", cols, filter) : prox.getNextBatch("AutomationInstance", reqID);
  
    Write('<br>data: '+ Stringify(data));

    if (data != null) {

        moreData = data.HasMoreRows;
        reqID = data.RequestID;

        if (data && data.Results) {

            for (var i = 0; i < data.Results.length; i++) {

              var autoName = data.Results[i].Name;
              var autoStatus = data.Results[i].Status;
              var autoStatusLastUpdate = data.Results[i].StatusLastUpdate;
              var autoStatusMessage = data.Results[i].StatusMessage;

              var data2 = {
                "AutomationName" : autoName,
                "AutomationStatus" : autoStatus,
                "AutomationLastUpdate" : autoStatusLastUpdate,
                "AutomationStatusMessage"  : autoStatusMessage
              }
              //Write(de.Rows.Add(data));
              Write('<br>AutomationName: ' + data2["AutomationName"]);
              Write('<br>AutomationStatus: ' + data2["AutomationStatus"]);
              Write('<br>AutomationLastUpdate: ' + data2["AutomationLastUpdate"] );
              Write('<br>AutomationStatusMessage: ' + data2["AutomationStatusMessage"]);
           }

        }
    }
}
</script>

Output:


data: {"Status":"OK","RequestID":"06a7a891-73db-4ea9-a041-2ea49d33e95e","Results":[{"AutomationID":null,"StatusMessage":"Complete","StatusLastUpdate":"2022-04-01T10:59:10.750","TaskInstances":null,"StartTime":"0001-01-01T00:00:00.000","CompletedTime":"0001-01-01T00:00:00.000","Schedule":null,"AutomationTasks":null,"IsActive":false,"AutomationSource":null,"Status":1,"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":"DataExtension1 Import","Description":null,"Keyword":null,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"ObjectID":null,"CustomerKey":null,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"AutomationID":null,"StatusMessage":"Complete","StatusLastUpdate":"2022-04-05T12:55:35.432","TaskInstances":null,"StartTime":"0001-01-01T00:00:00.000","CompletedTime":"0001-01-01T00:00:00.000","Schedule":null,"AutomationTasks":null,"IsActive":false,"AutomationSource":null,"Status":1,"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":"DataExtension1 Import","Description":null,"Keyword":null,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"ObjectID":null,"CustomerKey":null,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"AutomationID":null,"StatusMessage":"Complete","StatusLastUpdate":"2022-04-13T16:32:47.390","TaskInstances":null,"StartTime":"0001-01-01T00:00:00.000","CompletedTime":"0001-01-01T00:00:00.000","Schedule":null,"AutomationTasks":null,"IsActive":false,"AutomationSource":null,"Status":1,"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":"DataExtension1 Import","Description":null,"Keyword":null,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"ObjectID":null,"CustomerKey":null,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"AutomationID":null,"StatusMessage":"Complete","StatusLastUpdate":"2022-04-13T16:35:12.910","TaskInstances":null,"StartTime":"0001-01-01T00:00:00.000","CompletedTime":"0001-01-01T00:00:00.000","Schedule":null,"AutomationTasks":null,"IsActive":false,"AutomationSource":null,"Status":1,"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":"DataExtension1 Import","Description":null,"Keyword":null,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"ObjectID":null,"CustomerKey":null,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}, {"AutomationID":null,"StatusMessage":"Complete","StatusLastUpdate":"2022-04-14T08:16:30.597","TaskInstances":null,"StartTime":"0001-01-01T00:00:00.000","CompletedTime":"0001-01-01T00:00:00.000","Schedule":null,"AutomationTasks":null,"IsActive":false,"AutomationSource":null,"Status":1,"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":"DataExtension1 Import","Description":null,"Keyword":null,"Client":null,"PartnerKey":null,"PartnerProperties":null,"CreatedDate":"0001-01-01T00:00:00.000","ModifiedDate":null,"ID":0,"ObjectID":null,"CustomerKey":null,"Owner":null,"CorrelationID":null,"ObjectState":null,"IsPlatformObject":false}],"HasMoreRows":false}
AutomationName: DataExtension1 Import
AutomationStatus: 1
AutomationLastUpdate: Fri, 01 Apr 2022 10:59:10 GMT-06:00
AutomationStatusMessage: Complete
AutomationName: DataExtension1 Import
AutomationStatus: 1
AutomationLastUpdate: Tue, 05 Apr 2022 12:55:35 GMT-06:00
AutomationStatusMessage: Complete
AutomationName: DataExtension1 Import
AutomationStatus: 1
AutomationLastUpdate: Wed, 13 Apr 2022 16:32:47 GMT-06:00
AutomationStatusMessage: Complete
AutomationName: DataExtension1 Import
AutomationStatus: 1
AutomationLastUpdate: Wed, 13 Apr 2022 16:35:12 GMT-06:00
AutomationStatusMessage: Complete
AutomationName: DataExtension1 Import
AutomationStatus: 1
AutomationLastUpdate: Thu, 14 Apr 2022 08:16:30 GMT-06:00
AutomationStatusMessage: Complete
1
  • Ah yes - that would explain it! As you can see from my second example I was originally using response as my var name for the retrieve but then switched it to data to follow the documented example but didn’t notice the clash. Doh! Thanks!
    – JimS
    Commented Apr 19, 2022 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .