top of page
Forum Posts
Tim Wetzel
May 17, 2024
In Velo (Wix Code)
I have a code that takes a form's email field, looks up the associated contact on the wix website and then autofill other fields on the same form.
I have a temporary collection called AllContacts (see image).
When I test the code and type "tim11@server.com" it returns "no contact" It is definitely there.
the page code I have follows:
import wixCRM from 'wix-crm';
import wixData from 'wix-data';
//import wixForms from 'wix-forms';
$w.onReady(function () {
const emailField = $w('#emailInput');
const firstNameField = $w('#firstName');
const lastNameField = $w('#lastName');
emailField.onBlur(() => {
const email = emailField.value;
if (email) {
lookupContactByEmail(email)
.then(contact => {
if (contact) {
firstNameField.value = contact.firstName;
lastNameField.value = contact.lastName;
} else {
console.log('No contact found with that email.');
}
})
.catch(error => {
console.error('Error looking up contact:', error);
});
}
});
});
function lookupContactByEmail(email) {
return wixData.query('AllContacts')
.eq('primaryInfo.email', email)
.find()
.then(results => {
if (results.items.length > 0) {
return results.items[0];
} else {
return null;
}
})
.catch(error => {
console.error('Error querying Contacts:', error);
throw error;
});
}
Thanks for the help!
0
1
15
Tim Wetzel
More actions
bottom of page