I have a Google form linked to my Google sheets spreadsheet and an onFormSubmit
trigger added to run some code when a form response is received. I've been experiencing since quite a while that at random times the trigger decides to run two times, sending two emails to the same user (upon one form submission). Here's a screenshot of the Triggers tab in my Apps Script Project -
The onFormTrigger's job in my apps script project is to run a sendEmail
function I have which calls an AWS API on my website to send an email with a randomly generated 6 digit Activation key to the address user entered in the google form submission. So the random time double running of the trigger is making things messy as some of my users are receiving two emails w/ two keys so it's confusing for them too.
Here's a screenshot of Executions tab, showing an example of a double-run incident from today - the sendEmail
function twice in a 30s bracket due to onFormTrigger malfunctioning. (both emails below the red arrow are same) -
Is there anything I can do to prevent this? Here's what my sendEmail
function code roughly looks like -
function sendemail(e) {
var data = e['values'];
Logger.log(data);
const email = data[2];
var name = data[0];
var actcode = ; //code to generate the key here
//more code here...
var dataarr = [actcode,name,data[1],email,data[3],expiry,0,data[4],date,expiry,version2,0,"Activated","No",combine2];
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].appendRow(dataarr);
//above line adds form entry in different sheet
//the error causes 2 entries being added sometimes
var paramslist = [email,name,actcode,date,expiry,version,link];
var params = paramslist.join(" | ");
var finalurl = "https://mywebsite.com/awsdata/sendemail.php?data=" + encodeURIComponent(params);
//my website has aws files hosted on it
//and sendemail.php takes data from the form response and sends email using aws ses code I've added to it
var response = UrlFetchApp.fetch(finalurl);
Logger.log(response.getContentText());
//Logger also two entries due to the bug, within 30s span
}
The twice running of trigger doesn't show a pattern, just randomly for 3-4 form submissions out of 30-40 entries. What could be done here to prevent this spurious behaviour of onFormSubmit trigger? Thanks.
P.S. I've read 2-3 posts about related to this kind of issue here, but couldn't figure out how can this be fixed / prevented... Please advice here what I should do...