I created a Google Form and I want it to call my webhook. I prefer this approach to monitoring all the time because I won’t be getting that many responses, and people can edit their responsse later.
I set the option to collect emails to “true.” I do it so people can go back and edit their responses. I’m also not asking them to be logged into their Google account because they may not have one.
I’ve set up my webhook and an onSubmit() method to call it when a user submits a response.
This is my code:
var POST_URL = "https://hook.us1.make.com/my-webhook-url";
function onSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var response = latestResponse.getItemResponses();
var payload = {};
for (var i = 0; i < response.length; i++) {
var question = response[i].getItem().getTitle();
var answer = response[i].getResponse();
payload[question] = answer;
}
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(POST_URL, options);
};
However, when I get the response in the webhook, the email isn’t there. Is there a way to get it? Or is my only option to collect the email manually in my form?
If I take that route, will the user still be able to modify their response after-the-fact?
I finally figured it out. This is the correct code to use:
var POST_URL = "https://hook.us1.make.com/sat3e4cemi0u1woulp5ui8g7hc6kujgq";
function onSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var response = latestResponse.getItemResponses();
var payload = {};
for (var i = 0; i < response.length; i++) {
var question = response[i].getItem().getTitle();
var answer = response[i].getResponse();
payload[question] = answer;
}
👉🏽var email = latestResponse.getRespondentEmail(); // You get the respondent from the response, not the form 👈🏽
payload["email"] = email;
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(POST_URL, options);
};