Google form onSubmit trigger event response is empty

Viewed 708

I have a Google form linked to a trigger. when an onSubmit event is triggered the I'm getting an empty response object

function setupTriggers() {
  ScriptApp.newTrigger('onSubmit')
  .forForm('1n78vh9LfdUBCdKY4hcMZXEyYZw7ZxdE689-ihhczPow')
  .onFormSubmit()
  .create();
}


function onSubmit(e) {
  console.log(JSON.stringify(e));
}

Log

{"authMode":"FULL","response":{},"source":{},"triggerUid":"1655010400613789390"}

When a form is submitted the event gets triggered, but the event response is empty.

Am I missing any steps while configuring the trigger?

2 Answers

Try something like this:

function setupTriggers() {
  ScriptApp.newTrigger('onMyFormSubmit').forForm(FormApp.getActiveForm()).onFormSubmit().create();
}

function onMyFormSubmit(e) {
  console.log(JSON.stringify(e));
  var formResponse = e.response;
  var itemResponses = formResponse.getItemResponses();
  itemResponses.forEach(r=>{
    console.log('Id: %s\nType: %s\nTitle: %s\nResponse: %s\n',r.getItem().getId(),r.getItem().getType(),r.getItem().getTitle(),r.getResponse());
  });
 
}

I just have two questions on my form and here is what the executions look like for that form:

Cloud logs
May 4, 2021, 3:39:39 PM Debug   {"authMode":"FULL","response":{},"source":{},"triggerUid":"7825823049830695122"}
May 4, 2021, 3:39:39 PM Debug   Id: 1518756049
Type: MULTIPLE_CHOICE
Title: COL1
Response: 6
May 4, 2021, 3:39:39 PM Debug   Id: 1430799996
Type: MULTIPLE_CHOICE
Title: COL2
Response: 2

The response object is not empty. Using the below references you can still get your responses. But it would be a lot easier to get these if you would look at the onformSubmit trigger for the spreadsheet. It's event object is much more accessible.

Interface Item Methods

Class ItemResponse

Google Sheets Events

The event object is not empty, it's just that some of the objects, namely FormResponse and Form, do not have properties that can be stringified.

You would have to access the relevent data in these objects using the appropriate methods indicated in the linked documentation.

Related