All,
I’m trying to build a simple bit of code for a basic sales funnel on my site. The workflow is for the user to enter their email address into a contact form, on email submit, the code will scan a data collection to find the next unused coupon code (a custom DB I created). One the code is identified, I want to create a new contact with just he email address and then the new coupon code placed in a custom field (field already exists in my contact list). The code seems to be throwing an error that I can’t track down. What’s really throwing me off is that when I run tests by making up new emails (even fake ones) the contact list is bringing back a dummy contact that I have deleted and appending the new email as ANOTHER email to a contact that was already previously deleted. This occurs rather the site is in preview or published. Please if anyone can provide any help I would appreciate it. Here’s the code from the email submission form. Please note that I’m not a professional programmer, more of a hobbyist and this work is for my own site. I really appreciate it!
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixCRM from ‘wix-crm’;
import wixWindow from ‘wix-window’;
import wixData from ‘wix-data’;
import {session} from ‘wix-storage’;
import wixLocation from ‘wix-location’;
$w.onReady( function () {
var couponcode = “Null”
$w(“#dataset1”).onReady( () => {
wixData.query(“AMZ_CouponCodes_50off”)
.eq(“codeUsed”, false )
.find()
.then( (results) => {
if (results.items.length > 0) {
let item = results.items[0];
item.codeUsed = true ; // updated codeUsed boolean operator to track coupons that were distributed
wixData.update(“AMZ_CouponCodes_50off”, item);
couponcode = item.promoCode;
session.setItem(“StoredCoupon”, couponcode) //store the coupon for this session to use in thank you page and pass to msgr bot
console.log(couponcode);
} else {
//handle case where no matching items found
}
} )
. catch ( (err) => {
let errorMsg = err;
});
// now lets send an email to the users with the coupon code using the contact records email info
$w(“#button1”).onClick( () => {
let contactRecord = {
“emails”: [$w(“#input2”).value],
“amzcode”: couponcode,
“msgrid”: “New Null”,
};
console.log(“createContact(”+JSON.stringify(contactRecord)+“)”); //console shows correct input data
wixCRM.createContact(contactRecord)
.then( (contactId) => {
wixCRM.emailContact("getemaillbr", contactId, {
“variables”: {
“couponcode”: couponcode,
}
} );
} );
} );
} );
});
Thank you,
Lance