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;
}