Let’s trigger a custom action after Google Form submission

Let’s image you are collecting responses with a Google Form. You may want to send automatically an email right the user submitted his responses.

You are on the right place. Let’s build this using a custom Google AppScript function and the Triggers feature.

Step 1: Open the Google Script Editor

→ Go to your form
→ Click the 3 dots on top right corner of your screen (see screenshot below)
→ Click Script Editor
→ You will switch to a new tab with an url looking like script.google.com/…

Script editor

Step 2: Create the empty function

It’s time to create a new function, which will be empty for the moment but don’t worry, we will write the functions’ code in step 4.

Just write the code below in the Code.gs file (you may already be in the file, it’s automatically created when you open the Script editor).

function onSubmit() {
// code to be added here later...

Step 3: Create & connect a new trigger

You have a create a new trigger and connect it to our empty function we just created.

→ Click the Triggers section in the left panel

→ Click the Add Trigger blue button in the right bottom corner on your screen

Now we will configure the new trigger to connect it with the empty function.

→ Choose which function = onSubmit
→ Deployment = Head
→ Event source = From form
→ Event type = On form submit
→ Failure notification = Notify me immediately

Step 4: Write the code of the function

Now you can go back to the Script editor by clicking Editor in the left panel.

In this example, I will show you how to send automatically an email to the user, right after he submitted the form.

Let me explain the code, line by line.

function onSubmit() {
  // Get active form & latest response
  var oForm = FormApp.getActiveForm();
  var aResponses = oForm.getResponses();
  var oResponse = aResponses[aResponses.length-1]; //latest response

  //  Find user email
  var userEmailAuto = oResponse.getRespondentEmail();
  // or...
  var userEmailManual = oResponse.getItemResponses()[5]; // imagine the user entenred his email in question number 6

  // User replies to insert in email
  var result = oResponse.getItemResponses()[2].getScore();

  // Send an email to the user
    to: userEmailAuto, // or userEmailManual
    subject: "[RESULT] Following form submission",
    htmlBody: "Thanks for submitting the form.<br>" + 
              "Following your score (" + result + ") on question 3, you are allowed to join our team.<br>" +
              "🎉 Congrats"

  // End of the script


Step 5: Test the execution

Two ways to test the code:

  1. You can debug and run it using the Script editor by clicking the Run or Debug button in the tool bar.
  2. Simply by submitting the form 😉




Any suggestion, anything to add? Write a comment!