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?
When servers are busy, these race conditions will become more pronounced, but they are simply business as usual for cloud computing. In a nutshell, each user of a document has a view (copy) of that document, which cannot be guaranteed to be identical to a "master version" all the time. When you look at a spreadsheet, you are looking at your own copy of that spreadsheet. Your collaborator might be looking at their own copy. A trigger function accessing "the spreadsheet" will, in fact, be given its own copy as well. Changes made anywhere need to be synchronized everywhere, and that takes time.
In this case your code indicates that you have a function in a script that is contained in a Google Form. That script, when triggered, will be given a copy of the Form, including past responses. However, the form submission that triggered the script may not be synchronized with the form submission yet. You're also working with a spreadsheet that contains responses... when forms are submitted to the Form Service, they are stored in the Forms Service and they are also stored in a copy of the spreadsheet. That action may trigger a Spreadsheet Form Submission event, and (is your head sore yet?) that function will be given a copy of the spreadsheet that might not yet contain the new form submission data!
So, what to do?
Let's assume you're using a trigger function to handle form responses.
function handleForm( event ) {
...
}
If you only need to process the "current form response", you should use the event information that is handed to the trigger, rather than reading the spreadsheet or querying the form responses. Read over Understanding Events to see what event information is provided to the specific type of trigger you're dealing with. (Added bonus: using the event parameter saves you from calling the services APIs, which makes your function faster.)
function handleForm( event ) {
var formResponse = event.response; // The response that triggered this
// function is in the event
...
}
I suggest you also take a look at "How can I test a trigger function in GAS?".