Use Google Apps Script to send notifications to different recipients based on a form value (e.g. organisation).
Aim
It is fairly straightforward to set up a form for information collection in Google Apps, but often the ensuing proliferation means that important responses may get overlooked and missed. It is easy to set up an email notification for new responses, but these only go to the form owner. For more complex implementations, you may wish to be able to ‘route’ these notifications to different email addresses based on a particular form value.
Imagine you set up a generic booking form, with a drop-down question that allowed the respondent to select which event they were booking. In your organisation, different people are responsible for different events, so you want different individuals to be notified when a booking is submitted for their event. This code does just that.
Method
Set-Up your Form and Response Sheet
Either create a new form, or use an existing one. You’ll need to store your form responses in a Google Sheet. You will also need to decide which question on your form acts as the ‘discriminator’ in deciding who receives the notification email (e.g. Organisation or Event).
On this sheet, you’ll need to create a new tab/sheet called ‘Notifications’ (if you’d like to name it something else, that’s fine, just update the code below). In this sheet, you’ll need a simple table that looks something like this:
[Field Name] | |
---|---|
Organisation A | a.person@example.com |
Organisation B | b.person@example.com |
Organisation C | c.person@example.com |
Organisation D | d.person@example.com |
Simply entitle the first column ([Field Name]) with the name of the ‘discriminator’ question that you have chosen. Then fill in possible values (normally the field will be a drop-down box on your form, so this list should match that one). For each option, fill in a destination email address too.
Add the Code
You’ll need to open the Script Editor from Tools -> Script Editor in your Google Sheet. Then you can paste the two sections of code in from below. Explanatory comments are provided within the code, to make it easier to understand and change within the script editor itself.
Set Up the Trigger
Once your code has been pasted in and saved, you now need to create a trigger to run the code every time a form response is submitted. You can do this in the script editor by selecting Resources -> Current project’s triggers. Then select handle_onSubmit to run when the form is submitted. Email notifications will be sent from the account you are using to do this, so at this point you’ll be asked to authorise the code to send email on your behalf.
Finally
Test, test & test! Use some familiar email accounts to check that it is working in the way you want it to, then put in the ‘real’ routing and watch those emails fly around. You’ll also be able to see them in your ‘sent email’ label.
Tagged ► Google, Sheets, Javascript
Related ► Google Calendars on the Web (01 Sep 2018), Protecting Data with Google Drive (14 Aug 2018), Dealing with confidential information in schools (24 Jul 2018)