'Specific form id submission - email trigger from spreadsheet data

Looking for a way to trigger email script when specific form eg.form1 is submitted.

I have 2 forms form1 & form2, responses of both the forms are captured in 2 different sheets of same workbook.

I have a code in google app script to trigger email on submission of the google form shared below. While the code suggests the code be triggered on submission of specific form with formid, but the script is triggered even if form2 is also submitted.

ScriptApp.newTrigger('myFunction')
.forForm('xyz')
.onFormSubmit()
.create();

function myFunction(e) {
 var spreadsheet = SpreadsheetApp.openById('14YicUUJeb3hopA7T7qydAN9ri__59_kmHfPpeURGlFg');
 var responseSheet = spreadsheet.getSheetByName('Form Responses 1');
 var rLastRow = responseSheet.getLastRow();
 var lastCol = responseSheet.getLastColumn();
 var values = responseSheet.getRange(rLastRow, 1, 1, lastCol).getValues()[0];

 var status = values[11];
 var ticketId = values[0];
 var timestamp = Utilities.formatDate(new Date(values[1]), 'GMT+6', 'dd/MM/yyyy HH:mm a');
 var requestSubject = values[2];
 var requestType = values[2];
 var requestPriority = values[3];
 var requestDescription = values[4];
 var staffEmail = values[6];
 var facilityType = values[7];
 var facilityCode = values[8]
 
 if(status=='Open'){
var subject = '[Ticket ID: ' + ticketId + '] ' + requestSubject;

// Email Text. You can add HTML code here - see ctrlq.org/html-mail
var htmlBody = 'Dear IRT Team,';
htmlBody += '<p>This is to notify that there is a new service request from Unimove IRT Helpdesk portal.</p>';
htmlBody += '<p><strong>Request Type:</strong> ' + requestType;
htmlBody += '<br><strong>Request Date:</strong> ' + timestamp;
htmlBody += '<br><strong>Priority:</strong> ' + requestPriority;
htmlBody += '<br><strong>Description:</strong> ' + requestDescription;
htmlBody += '<br><strong>Facility Type:</strong> ' + facilityType;
htmlBody += '<br><strong>Facility Code:</strong> ' + facilityCode;
htmlBody += '<br><strong>Staff Email:</strong> ' + staffEmail + '</p>';
htmlBody += '<p>Above details extracted from <a href="https://docs.google.com/spreadsheets/d/14YicUUJeb3hopA7T7qydAN9ri__59_kmHfPpeURGlFg/edit" target="_blank">Unimove IRT Helpdesk (Responses)</a>.</p>';
htmlBody += '<p>Thank you.</p>';
htmlBody += '<p>Regards,<br>Unimove IRT Helpdesk</p>';

GmailApp.sendEmail('[email protected]', subject, '', {htmlBody:htmlBody, name: 'Unimove IRT Helpdesk', replyTo: '[email protected]'}); }


Solution 1:[1]

Hey I believe you will just need to add another column to basically mark that the email has been sent before and if the "Sent Check" column is empty then send email. This will prevent duplicated emails.

Sorry if I got it wrong but from reading your code and your problem I understand that the lastRow entry of form 1 is sent before and when form 2 is submitted is will still send the same lastRow data in email?

Proposal:

 var sentcheck = values[12]; //Check if email has been sent before//
 var status = values[11];
 var ticketId = values[0];
 var timestamp = Utilities.formatDate(new Date(values[1]), 'GMT+6', 'dd/MM/yyyy HH:mm a');
 var requestSubject = values[2];
 var requestType = values[2];
 var requestPriority = values[3];
 var requestDescription = values[4];
 var staffEmail = values[6];
 var facilityType = values[7];
 var facilityCode = values[8]
 
 if(sentcheck=='SENT') return; //skip if sent before//
 if(status=='Open'){
var subject = '[Ticket ID: ' + ticketId + '] ' + requestSubject;

// Email Text. You can add HTML code here - see ctrlq.org/html-mail
var htmlBody = 'Dear IRT Team,';
htmlBody += '<p>This is to notify that there is a new service request from Unimove IRT Helpdesk portal.</p>';
htmlBody += '<p><strong>Request Type:</strong> ' + requestType;
htmlBody += '<br><strong>Request Date:</strong> ' + timestamp;
htmlBody += '<br><strong>Priority:</strong> ' + requestPriority;
htmlBody += '<br><strong>Description:</strong> ' + requestDescription;
htmlBody += '<br><strong>Facility Type:</strong> ' + facilityType;
htmlBody += '<br><strong>Facility Code:</strong> ' + facilityCode;
htmlBody += '<br><strong>Staff Email:</strong> ' + staffEmail + '</p>';
htmlBody += '<p>Above details extracted from <a href="https://docs.google.com/spreadsheets/d/14YicUUJeb3hopA7T7qydAN9ri__59_kmHfPpeURGlFg/edit" target="_blank">Unimove IRT Helpdesk (Responses)</a>.</p>';
htmlBody += '<p>Thank you.</p>';
htmlBody += '<p>Regards,<br>Unimove IRT Helpdesk</p>';

GmailApp.sendEmail('[email protected]', subject, '', {htmlBody:htmlBody, name: 'Unimove IRT Helpdesk', replyTo: '[email protected]'}); 

responseSheet.getRange(rLastRow, 1, 1, lastCol).setValue('SENT'); //mark as sent when email has been delivered//
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nami888