When using the FormResponse class, a unexpected Id is returned for a response depending on how the response is retrieved

StackOverflow https://stackoverflow.com/questions/22153344

  •  19-10-2022
  •  | 
  •  

Question

I'm hoping someone has seen this before, I'm using Google Forms, and on submission, using Google Apps Script to store the response id in a separate Spreadsheet in order to store further meta data in the future (Id gained from the method in the onSubmit event response e.response.getId()). Upon the first submission of a Google Form, I logged the response ID in a new sheet.

I can load the response through form.getResponse(responseIdStoredInSheet), however, when I loop through the responses returned by form.getResponses() and call .getId() on those FormResponses returned, the Id is slightly altered at the end. I've searched for an afternoon around the docs and online as to why this happens, but to no avail.

function testResponseIds()
{
  var responseIdStoredInSheet = 'ChMxNzM4MDQzNzQ5MjQyNDc0Njg4EAA';

  //only response in the form
  var formResponse = form.getResponse(responseIdStoredInSheet);
  //outputs as expected above - ChMxNzM4MDQzNzQ5MjQyNDc0Njg4EAA
  Logger.log(formResponse.getId());

  //loop through all responses (only above response is present)
  var formResponses = form.getResponses();
  for (var i = 0; i < formResponses.length; i++)
  {
    //Outputs slightly different Id - ChMxNzM4MDQzNzQ5MjQyNDc0Njg4EJ2kvMHLzsOvdg
    Logger.log(formResponses[i].getId());
  }
}

Logged output from above:

[14-03-03 17:12:51:259 GMT] ChMxNzM4MDQzNzQ5MjQyNDc0Njg4EAA
[14-03-03 17:12:51:279 GMT] ChMxNzM4MDQzNzQ5MjQyNDc0Njg4EJ2kvMHLzsOvdg
Was it helpful?

Solution

I tested your code on one of my form and it look to be working as intented. My supposition is that you deleted the answer you are trying to retrieve but you can still get it through the script(I don't know the reason, the cache I suppose).
What I can propose you is to retrieve the edit URL so you will see that it's not the same answer (modifying it and checking the form answers).

Logger.log(formResponses[i].getEditResponseUrl());

By the way in your posted script you forgot:

var form = FormApp.getActiveForm();

or the equivalent (couldn't run your script without adding that line)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top