Hi I was hoping someone might be able to help me with my code, i’m trying to get some variable templates through sendgrid to deliver with an onClick through a repeater. The template works as I have tested through onAfterUpdate. I’m just trying to get this to happen with a button click.
Backend
import {fetch} from 'wix-fetch';
import sgMail from '@sendgrid/mail';
import wixData from 'wix-data';
export function sendOutstandinglnvoiceEmail(){
sgMail.setApiKey('API KEY here');
wixData.query("CourseAvailability")
.find()
.then((results) => {
let item = results.items[0];
let fullname = item.fullName;
let courseTitle = item.title;
let totalPrice = item.totalPrice;
let outstandingBalance = item.outstandingBalance;
const msg = {
to: item.customerEmail,
from: "info@scottishrockandwater.com",
templateId: 'd-c68106a391a24fe0b08c22285fc2427e', //Outstanding Invoice
dynamic_template_data: {
name: fullname,
courseTitle: courseTitle,
totalPrice: totalPrice,
outstandingBalance: outstandingBalance,
},
};
sgMail.send(msg);
console.log(fullname)
console.log(courseTitle)
console.log(totalPrice)
console.log(outstandingBalance)
})
}
Frontend
import {sendOutstandinglnvoiceEmail} from 'backend/email';
//various repeater data above this hence the itemData
$w("#sendReminderButton").onClick((event) => {
sendOutstandinglnvoiceEmail(
itemData.customerEmail,
itemData.fullname,
itemData.title,
itemData.oustandingBalance,
itemData.totalPrice)
.then(function () {
console.log("email was sent");
})
.catch((err) => {
console.log("Error!");
});
})
Errors
I’m sure a few links will be sent over but I’ve been trying to achieve this for the 36 hours and I’m pretty confident I have read them I just can’t figure this out.
I feel close with this one just need to get the data transferred to send the email I think…
Thank you in advance and I hope this is clear enough to be able to help.