Sending triggered email to specific address- help with code please!

Hi all, I’m going to try to explain as best I can. I have a database of volunteers. User selects the volunteers they would like to meet, enters their name and email, and submits form. I now want to send a triggered email to my admin assistant with the array of volunteer names, user name, and user email (so that it looks nice for her). I’ve got the code working, the issue is that I get multiple emails on submit (one for every box checked). I’d like to just capture the finished array that has all items selected and then send the email–so that only one email is sent when user pushes submit-- but am lost on code. (**user and email ID not included for privacy) Any help is appreciated!

So basically an email sent that is something like:


Hi,
Here are the volunteers I’d like to meet:
<Brian Chung, Ashley Amerson, Marcus Smith>
please respond to me at < email@email.com >

thanks!


here is a pic of the page

here is my code:

import wixData from 'wix-data';
import wixUsers from 'wix-users';

let originalPropertiesInfo = [];
let myCollectedArray = [];

$w.onReady(function (repeaterData, Email) {
  
  repeaterData
   //Query to get the information from the database  
    wixData.query("Team")
    .find()
    .then((results) => {
       originalPropertiesInfo = results.items;
       $w(`#teamRepeater`).data = originalPropertiesInfo;
    })
    .catch((err) => {let errorMsg = err;});

    $w(`#teamRepeater`).onItemReady(($item, itemData, index) => {
        console.log("INDEX: ", index);
        console.log("ITEM_DATA: ", itemData);         
        //---------------------------------------------------------
        $item('#teamImage').src =           itemData.photo;
        $item(`#teamMemberName`).text =     itemData.title;
        $item('#jobTitle').text =           itemData.jobTitle;
        $item('#shortDescription').text =   itemData.shortDescription;
        $item('#text47').text =             itemData.generalAvailability;
        $item('#checkbox1').value =         'Meet me' + itemData.title;
        //---------------------------------------------------------------
        //create array and input to database
        $item("#checkbox1").onChange(()=>{  
            let isChecked = $item('#checkbox1').checked;
            if(isChecked) {console.log("Before --> PUSH! = ", myCollectedArray);
                myCollectedArray.push(itemData.title);    
                console.log("After --> PUSH! = ", myCollectedArray);
                $w("#dataset5").setFieldValue('volunteers', myCollectedArray);
                
                } else {isChecked = false}
            
              
        
       //---------------------------------------------------------------
       //send triggered email to admin
Email
  $w("#dataset5").onAfterSave(() => {
const user_Id = "xxx-xxx";
const email_ID = "yyy-yyy";

wixUsers.emailUser(email_ID, user_Id, {
    "variables": {
      "Name": $w('#input1').value,
      "email": $w('#input2').value, 
      "MyCollectedData": myCollectedArray.join($item(itemData.title) + "," + " ")}
})
  
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);} )
  })

});
});
 }); 

// clear selections from repeater on submit
export function button1_click(event, $w) {
$w("#checkbox1").checked = undefined;

}

After a short look onto your code, it is not surprising, that you get multiple emails, because you have setted it up inside the → onAfterSave

Every time you do a changment, your onAfterSave is beeing fired and you send an E-mail, right?

Surely you want first to do all changements, then press a submit-button to send just one email.

So you can save all values inside an array (like you already do), then you change the event which will activate the send-email-function.

Thanks!! that did it. Moved the send email function to button1_click event. I appreciate all your help with this, and the earlier coding help too!! Here is updated code for anyone doing something similar.


// clear selections from repeater on submit
export function button1_click(event, $w) {
$w("#checkbox1").checked = undefined;

//Email
  $w("#dataset5").onAfterSave(() => {
const user_Id = "xxx-xxx";
const email_ID = "yyy-yyy";

wixUsers.emailUser(email_ID, user_Id, {
    "variables": {
      "Name": $w('#input1').value,
      "email": $w('#input2').value, 
      "MyCollectedData": myCollectedArray.join(', ')}
})
  
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);} )
  })

}

Thanks for the answer.