I have looked at the help centre articles and forum comments on sending triggered emails via code and created a function in which an email should be sent, but it’s not working.
I first had to run the code to create a contact because it seems you can only use a contact ID within the triggered email API. When I did this everything within the function worked and it logged the ID on the console. However, it didn’t send the email.
I then blocked out the create contact code and added the contactID to the triggered email API and it’s still not working… I’m not sure what’s going wrong.
Any help would be great!
Here’s the code I used to create the function:
*note: yes, I have imported wixCRM and wixUsers at the top of the page code.
function sendCompanyNotification() {
if (($w("#programSelectDropdown").value==='Soccer')) {
let dobOptions = {day: "2-digit", month: "2-digit", year: "numeric"};
// Get current user.
let user = wixUsers.currentUser;
let userId = user.id;
user.getEmail()
.then((email) => {
let userEmail = email; // "user@something.com"
console.log("Current user email:", userEmail);
console.log(userId);
// Filter dataset to show items created by current user.
// get last item by current user.
wixData.query("soccerMembersDatabase")
.eq("email", userEmail)
.find()
.then( (results) => {
if(results.items.length > 0) {
let details = results.items[0];
let skillLevel = details.skillLevel
let memberName = details.firstName + " " + details.lastName;
let memberPhone = details.phone;
let memberEmail = details.email;
let memberAddress = details.address + ", " + details.city + ", " + details.postCode;
let memberDOB = details.birthDate.toLocaleDateString("en", dobOptions);
let gender = details.gender;
let ecName = details.emergencyContactName;
let ecPhone = details.emergencyContactPhone;
let ecRelationship = details.emergencyContactRelationship;
let goalkeeperStatus = details.goalkeeper;
let regRefNo = details._id;
let selectedProgram = details.program.toString();
let paymentDue = details.paymentDue;
console.log(details)
let emailId = "yoursite@georgianbaymedia.com"/*"info@georgianbaysportsclub.com"*/;
/*wixCRM.createContact( {
"firstName": "Gerogian Bay Media",
"lastName": "",
"emails": ["yoursite@georgianbaymedia.com"],
"phones": ["7056069093"]
} )
.then( (contactId) => {
console.log("GBM Contact ID = " + contactId )
} );*/
let contactId = "079ed602-6e71-44c2-9dca-bfb3e04b1958";
wixCRM.emailContact('SPOa7aP', contactId, {
variables: {
memberName: memberName,
eContactName: ecName,
regRefNo: regRefNo,
eContactPhone: ecPhone,
memberAddress: memberAddress,
skillLevel: skillLevel,
paymentDue: paymentDue,
memberContactRelationship: ecRelationship,
programSport: selectedProgram,
memberEmail: memberEmail,
programSelection: selectedProgram,
memberDOB: memberDOB,
goalkeeperStatus: goalkeeperStatus,
gender: gender
}});
}
} )
.catch( (err) => {
let errorMsg = err;
console.log("sendCompanyNotification Error:" + err);
} );
})
}