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?