Reputation: 11
I am new to using Google Scripts. My company is conducting the same live webinar training session several times throughout the month of July. But since we are capping each training session to 10 participants, we are using a unique go-to meeting link for each web-training to avoid attendees that chose a different session to accidentally fall into another session.
I put together this script based of the instructions I saw here from searching through stack overflow forums.
Note: I haven't created the webinar URLs yet, because i'm trying to figure out the reason for the errors I am getting, so I just use placeholder text where the URLs would go.
function onFormSubmit(event) {
// Get the responses into convenient variables.
var name = event.values[3]; // Question 3
var organization = event.values[2]; // Question 2
var email = event.values[1]; // Question 1
var allSessions = event.values[4] // Question 4, a comma-separated list,
.split(','); // which we turn into an array
// Loop over all expressed interests, sending webinar links
for (var session in allSessions) {
sendWebinarLink( name, email, allSessions[session] );
}
}
/**
* Determine the id for a form that matches the given survey (interest),
* and send an email to the respondent.
*/
function sendWebinarLink( name, email, date ) {
var webinarURL = null; // Will fill with appropriate WebinarURL
// Set webinarURL according to current value of ‘date’.
switch (date) {
case "Mon - July 9th 2018 - 10am-11am":
webinarURL = '1234567890abcdefghijklmnopqrstuvwxyz'; // Replace with real form ID
break;
case "Mon - July 9th 2018 - 3pm-4pm":
webinarURL = '1happyhappyhappyz'; // Replace with real form ID
break;
// and so on...
default:
// Error handling, or for "other"
break;
}
// Send an email for any interest with a survey form
if (webinarURL != null) {
// Build Email Body
var body = 'Dear '+name+',<br><br>'; // Dear John Doe,
body += 'Thanks for registering for the Datalink RAE web-based training.<br><br>';
body += 'You selected the following date and time: ' + date;
body += ', which you will access via your computer with the following unique address.<br><br>';
body += 'Please follow <a href="' +webinarURL+ '">this link</a> at the start of you training time.<br><br>';
body += 'Thank you!';
MailApp.sendEmail({
to: email,
subject: date,
htmlBody: body
});
}
}
The first error I get is:
TypeError: Cannot read property "values" from undefined. (line 3, file "Code")Dismiss
And at this point, I'm not sure how to fix it (since Question 3 is where I ask for the registrant's name).
This is the Google Form
This is the Google Sheet
Any advice would be greatly appreciated!
Upvotes: 1
Views: 360
Reputation: 5716
There's no 'values' property in the event object that is passed to the onFormSubmit() trigger. Please refer to the official documentation:
https://developers.google.com/apps-script/guides/triggers/events
You can get the instance of the FormResponse class by inspecting the "response" property of the event object:
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
for (var i = 0; i < itemResponses.length; i++) {
var response = itemResponses[i];
var questionTitle = response.getItem().getTitle();
var userInput = response.getResponse();
}
Also, the error sounds as if you are trying to execute the onFormSubmit() function directly from the editor, in which case the "event" parameter won't be available. For the event object to be generated, you must initiate the event = submit the form.
Upvotes: 0