Fix emailing the wrong event name for people who have booked for another event

Fix emailing the wrong event name for people who have booked for another event
I have a Google Apps Script that works on the Responses sheet from Google Forms used to receive bookings for events. The form permits people to book for more than one event, but frequently, people book for the same event twice by mistake. The script detects the duplicate booking by controlling the content of the email column (1) and the event name column (7) and if it finds a duplicate, sends an email to the person who booked. The email contains the name of the event that the person had double-booked. The script is working fine when the person hasn't booked for any other events, but is emailing the wrong event name for people who have booked for another event. Example: email@email.com Event 1 email@email.com Event 2 email@email.com Event 2 Email is sent referring to double booking of Event 1. The script: function checkDuplicateOnSubmit(e) { var sheet = e.range.getSheet(); var submittedRow = e.range.getRow(); var lastRow = sheet.getLastRow(); var lastCol = sheet.getLastColumn(); // Read all data at once (faster than repeated getRange calls) var data = sheet.getRange(1, 1, lastRow, lastCol).getValues(); // Column B = index 1, Column H = index 7 (0-indexed) var EMAIL_COL = 1; var MATCH_COL = 7; var NAME_COL = 2; var submittedRowValues = data[submittedRow - 1]; // -1 because array is 0-indexed var submittedEmail = submittedRowValues[EMAIL_COL]; var submittedValue = submittedRowValues[MATCH_COL]; var submittedName = submittedRowValues[NAME_COL]; // Skip if either field is empty if (!submittedEmail || submittedValue === "" || submittedValue === null) { return; } var isDuplicate = false; // Assume row 1 is headers; check all rows except the one just submitted for (var i = 1; i < data.length; i++) { var rowNum = i + 1; // convert back to 1-indexed sheet row if (rowNum === submittedRow) continue; var existingEmail = data[i][EMAIL_COL]; var existingValue = data[i][MATCH_COL]; if (existingEmail === submittedEmail && existingValue === submittedValue) { isDuplicate = true; break; } } if (isDuplicate) { sendDuplicateWarningEmail(submittedEmail, submittedName, submittedValue); } } function sendDuplicateWarningEmail(email, name, matchValue) { var subject = "Duplicate Booking Detected"; var body = "Dear " + name + ",\n\n" + "We noticed you have already booked for the retreat entitled:\n\n" + matchValue + ".\n\n" + "If this was intentional (for example, you are booking on someone else's behalf), you can disregard this message. If not, " + "please reach out to us to correct your booking.\n\n" + "Thank you.\n\n" + "Fr Christopher Vella\n\n" + "Centre for Ignatian Spirituality"; MailApp.sendEmail(email, subject, body); } What is wrong with this script?

Take Your Experience to the Next Level

New

Download our mobile app for a faster and better experience.

Comments

0
U

Join the discussion

Sign in to leave a comment

0:000:00