Help modifying Backend triggered email code

I am trying to create a backend function that will send a triggered email to a specified contact (not a member). The frontend function is fairly straightforward, but I’m having trouble modifying (or even understanding) the more obtuse Wix backend code example.

What I’d like to do is call the backend function with three predefined parameters:
contactData” is a data array to fill the variables in the triggered email
contactCode” is the ID of the contact, eg. “a6c1170c-fa3a-41da-923f-7e0eb09d1781”
emailCode” is the triggered email ID, eg. “U3NJnZb”
Backend call should return a success flag to confirm that email was sent


On the front End: (my code)

import { sendEmailToContact } from 'backend/sendEmailToContact.jsw';
// [...]
sendEmailToContact(**contactCode**, **contactData**, **emailCode**)
  .then(success => {
         console.log(success);
     })
     .catch(error => {
         console.log(error);
     });

On the backend: (Wix sample code, verbatim for now)

import { Permissions, webMethod } from 'wix-web-module';
import { triggeredEmails } from 'wix-crm-backend';

export const myEmailContactFunction = webMethod(Permissions.Anyone, (emailId, contactId, options) => {
  return triggeredEmails.emailContact(emailId, contactId, options)
    .then(() => {
      console.log('Email was sent to contact');
    })
    .catch((error) => {
      console.error(error);
    });
});

How to modify the backend code to work with the frontend so that the email is sent according to the three parameters and a success flag is returned?

We don’t write custom code for users on this forum but if you need that support then maybe try the marketplace: https://www.wix.com/marketplace

I gave up trying to modify the Wix sample code, but found that this backend code works for me. Hopefully it can help others.

import {contacts, triggeredEmails} from ‘wix-crm-backend’;

export async function EmailVoucher(emailToFind,data) {
let contactID;
let success;
let emailID = ‘U3NJnZb’ // can replace with the code for whatever triggered email you want to send. Could also make it one of the function parameters.

let options = {
variables: {
“Firstname”: data.firstName,
“Lastname”: data.lastName,
// add your options here in this format
}
};
const queryResults = await contacts.queryContacts()
.eq(“info.emails.email”, emailToFind)
.find ({suppressAuth: true});
const contactsWithEmail = queryResults.items;

if (contactsWithEmail.length >= 1) {
contactID = contactsWithEmail[0]._id;
console.log("Found contact info for " + emailToFind);
} else {
console.log("No contacts found for " + emailToFind);
// Can add code to handle when no contacts found
}

try {
await triggeredEmails.emailContact(emailID, contactID, options);
console.log("Email successfully sent to contact " + contactID);
success = “yes”
} catch (error) {
console.error(error);
// Can add code to handle the error
}
return success
}

Just a small note …… if a Contact is marked as Unsubscribed no email will be sent out.

So at times it may seem like the code doesn’t work, when in reality it probably is just fine.

2 Likes