Wix Triggered email - error TypeError: Preflight response is not successful

I have a pretty straight forward triggered email as shown below. When the email is sent I get the following error "TypeError: Preflight response is not successful. Below is the snippet of code. Here’s my log trace:

Loading the code for the Contacts page. To debug this code, open i6kjt.js in Developer Tools.

Log output:

create contact.
Done
contact created.
contactId : eb3d37ab-5d92-4d8a-b238-4d4e8c321fa6
Sending emailContact
TypeError: Preflight response is not successful


Code below:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixCRM from ‘wix-crm’ ;

$w.onReady( function () {

})

export function button1_click(event) {
// Add your code for this event here:
// TODO: write your page related code here…
console.log( “create contact.” );
wixCRM.createContact({
“firstName” : $w( ‘#firstName’ ).value,
“lastName” : $w( ‘#lastName’ ).value,
“email” : $w( “#email” ).value,
“phone” : $w( ‘#phone’ ).value,
“message” : $w( ‘#message’ ).value
})
.then((contactId) => {
console.log( “contact created.” );
console.log( "contactId : " + contactId);

console.log( “Sending emailContact” );
wixCRM.emailContact( ‘[MY CODE]’ , contactId, {
variables: {
firstName: $w( ‘#firstName’ ).value
}})
.then(() => {
console.log( “email sent to contactId” );
// do something after the email was sent
})
. catch ((err) => {
console.log(err);
});

})
. catch ( (err) => {
console.log(err);
})
console.log( “Done” );
}

Hi …

Can you please provide us a link to your website so we can better inspect the issue?

Here’s the link to the page. Note that I’m trying to demo out triggered emails first before I add it to the existing site. This example is very simple. What I am observing is it is not sending the triggered email to the contact created but instead sending the email to the sender (me) instead. Why would that be?

https://www.ancoretours.com/contacts2

I wrote out some debug statements on the screen in the link https://www.ancoretours.com/contacts2

// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixCRM from ‘wix-crm’ ;
import {session} from ‘wix-storage’ ;

$w.onReady( function () {
$w( “#contactId” ).text = session.getItem( “contactId” );
$w( “#emailsent” ).text = session.getItem( “emailsent” );
$w( “#emailerror” ).text = session.getItem( “emailerror” );
$w( “#generalerror” ).text = session.getItem( “generalerror” );
})

export function button1_click(event) {
// Add your code for this event here:
// TODO: write your page related code here…
console.log( “create contact.” );
wixCRM.createContact({
“firstName” : $w( ‘#firstName’ ).value,
“lastName” : $w( ‘#lastName’ ).value,
“email” : $w( “#email” ).value,
“phone” : $w( ‘#phone’ ).value,
“message” : $w( ‘#message’ ).value
})
.then((contactId) => {
session.setItem( ‘contactId’ ,contactId);
console.log( “contact created.” );
console.log( "contactId : " + contactId);
$w( “#contactId” ).text = contactId;

console.log( “Sending emailContact” );

wixCRM.emailContact( ‘SCK8IAs’ , contactId)
.then(() => {
session.setItem( ‘emailsent’ , ‘yes’ );
$w( “#contactId” ).text = ‘yes’ ;
console.log( “email sent to contactId” );
// do something after the email was sent
})
. catch ((err) => {
session.setItem( ‘emailerror’ , err);
$w( “#emailerror” ).text = err;
console.log(err);
});

})
. catch ( (err) => {
session.setItem( ‘generalerror’ , err);
$w( “#generalerror” ).text = err;
console.log(err);
})
console.log( “Done” );
}

Hi there … I’ve already inspected the website and couldn’t find anything wrong, I may be able to find out what the problem is if I have access to the editor.

If you want me to take a deeper look, you can DM me to send me an admin invitation.

Note : You should always be careful about the identity of the entity that you’re inviting to become an admin, whether they’re trust worthy or not.

Ahmad what I am observing is that after creating the contact it keeps on returning the same contact ID which is the sender’s contact id instead of the recepient. The contact ID is " 3f9bc301-8756-4402-981d-22a43514539f" always no matter who the recipient is. Is this the correct way to obtain the recipients contact id when creating a contact?

wixCRM.createContact({
“firstName” : $w( ‘#firstName’ ).value,
“lastName” : $w( ‘#lastName’ ).value,
“email” : $w( “#email” ).value,
“phone” : $w( ‘#phone’ ).value,
“message” : $w( ‘#message’ ).value
})
.then((contactId) => {
session.setItem( ‘contactId’ ,contactId);
console.log( “contact created.” );
console.log( "contactId : " + contactId);
$w( “#contactId” ).text = contactId;
}

Yes, this is best way to get the contact ID.

The problem is probably something else.

What’s the best way to DM you?

Use the link in my profile to visit my website, and use the chat to contact me.

Ok here’s what I found out. Using the wixCRM createContact and emailContact only works for Members and users who have signed in. If your website does not have a current user session you will get the error " contactId does not match the current session".
You have to use the wix-crm-backend wix-crm-backend - Velo API Reference - Wix.com

I got it to work by doing the following. I created a crmcontact.jsw in the Backend as shown below:

/*****************************

  • backend code - events.js *
    *****************************/

import wixCrmBackend from ‘wix-crm-backend’ ;

export function CreateCRMContact(firstName, lastName, email, phone, message) {
return wixCrmBackend.createContact({
“firstName” : firstName,
“lastName” : lastName,
“emails” : [email],
“phones” : [phone],
“message” : message
})
.then((result) => {
const contactId = result;
return contactId;
});
}

export function EmailContactByID(emailCode, contactId) {

wixCrmBackend.emailContact(emailCode, contactId)
.then( () => {
// email has been sent
} )
. catch ( (err) => {
// there was an error sending the email
} );
}


My site page which does not use a wix form but a custom form, I then call my backend functions as how I did with the wixCRM as shown below:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import {CreateCRMContact} from ‘backend/crmcontact’ ;
import {EmailContactByID} from ‘backend/crmcontact’ ;

$w.onReady( function () {
$w( ‘#dataset1’ ).onReady(() => {
let firstName, lastName, email, phone, message

    $w( '#dataset1' ).onBeforeSave( **async**  () => { 
        firstName = $w( '#firstName' ).value; 
        lastName = $w( '#lastName' ).value; 
        email = $w( '#email' ).value; 
        phone = $w( '#phone' ).value; 
        message = $w( '#message' ).value; 

    }) 

    $w( '#dataset1' ).onAfterSave( **async**  () => { 

let contactDetails = {
“firstName” : firstName,
“lastName” : lastName,
“emails” : [email],
“phones” : [phone]
}

await CreateCRMContact(firstName, lastName, email, phone).then( async (contactId) => {
await EmailContactByID( ‘SCT6C4A’ , contactId). catch (err => console.error(err))
}). catch (err => console.error(err))
})

}) 

})