0
votes

I have a google form that asks up to 40 questions with a script firing on the "On form submit" event.

Most of the time I successfully reference the currently submitted response to var formResponse via this code which gets an array of all of the responses and then the most current via .length-1:

var formResponses = form.getResponses();
var i = formResponses.length-1;
var formResponse = formResponses[i];

It seems that when the form collects 25+ responses there is a processing delay and the script will run but the response has somehow not yet been added to the .getResponeses() array. The result is that the script runs again for an older response. By the time I go to look at the form and the spreadsheet containing the results it has been updated as expected but the event has already fired on the incorrect response...so I am wondering if I am referencing correctly or if there is an issue on the google form side. The issue is alleviated temporarily when I Delete all responses from the form but comes up again once the number of responses has reached approximately 25.

Thanks for your support!

1
All documentation indicates the above code is valid. The issue occurs once the form has collected 25+ responses and so I have introduced a delay in the script to allow google to finish posting the response to the array before I reference it. I am using ` Utilities.sleep(30000);` to wait 30 secs before executing the rest of the script.user2773881

1 Answers

1
votes

In any Google Apps Script Trigger function, you are best off using Event information that is handed to your trigger, thereby avoiding the timing issues altogether. Your trigger will have its own private copy

See Understanding Events.

For example, in a script that's attached to a Form:

function handleFormSubmission( event ) {
  var formResponse = event.response;
  ...
}

Similarly, in a script that's attached to a Spreadsheet that receives form responses:

function handleFormSubmission( event ) {
  // Array with values in the same order as they appear in the Spreadsheet.
  var values = event.values; 

  // An object containing the question names and values from the form submission.
  var namedValues = event.namedValues;
  ...
}