I'm wondering if this has to do with particularly busy times for Google Apps Script, because it seems like it has to do with an (occasional) delay in updating the length of a formResponse[] array. I'm using the following code to get the latest response triggered by a form submit:
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1]; //latest response only
Logger.log('begin length: ' + formResponses.length);
Then the rest of my script interacts with the answers in the formResponse[] array. Occasionally, I'll notice that it has gotten the response before the latest response. I can verify this because the spreadsheet with the form responses shows the actual latest response. My script takes 5-15 seconds to execute, so I I have the following lines at the end of my code to double check the length of the array again:
var form2 = FormApp.getActiveForm();
var formResponses2 = form2.getResponses();
Logger.log('end length: ' + formResponses2.length);
and in the log I'll notice that the second one is one greater than the first (and the second one is the correct value). I haven't really found much pattern as to when it happens, but it seems to happen more often between the hours of 7-9am PST. For now I've added a Utilities.sleep(5000)
as the first line in the function to allow time for the form to update before I get the responses, and so far I haven't had any n-2 responses, which makes me think there is some sort of delay causing the form to record the latest response after the "on form submit" trigger fires.
Has anyone else encountered anything similar?