Hot do I get a Google Form's respondent email?

Hi,

I’m not sure this is the right place to ask, but…

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 wonder if the problem isn’t related to this:

Thanks,

L

According to javascript - Extract Respondent Email from Google Forms Responses using Google Apps Script - Stack Overflow,

You cannot access their email unless you have set Form.setCollectEmail(true), which you have.

So you probably just need to use

var email = form.getRespondentEmail();

For more information, see getRespondentEmail()

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

1 Like

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);
};

Thanks!

L

3 Likes