-1

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 -

enter image description here

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

enter image description here

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...

Mr Shane
  • 520
  • 5
  • 18
kartik
  • 550
  • 1
  • 6
  • 19
  • 1
    You can check the executions tab to see if the function was the one that was running twice or not. – Century Tuna Aug 30 '23 at 13:43
  • @CenturyTuna I've edited my original post & added a screenshot of executions tab, showing an example of a double execution from today. Yes, it is the same `sendEmail` function... Please check - Thanks! – kartik Aug 30 '23 at 14:15
  • 1
    Pretty hard to say, since this happens intermittently - might be a spurious trigger issue dated back before. You can check this post if it fits your scenario. [https://stackoverflow.com/questions/72224026/google-apps-script-trigger-function-runs-twice](https://stackoverflow.com/questions/72224026/google-apps-script-trigger-function-runs-twice) – Century Tuna Aug 30 '23 at 14:36
  • 1
    Can you collect a Logger.log(JSON.stringify(event object)) for both triggers – Cooper Aug 30 '23 at 16:42

0 Answers0