I have a custom members area on my website (NOT using Wix app).
Using a Lightbox, users register with an email+password. This registers them as a wixUser AND inserts a item in a ‘client’ collection with the same id and email. This is the code within the onReady:
$w('#registe').onClick( **function** () {
$w('#error').hide()
let email = $w(‘#email’).value;
let password = $w(‘#password’).value;
wixUsers.register(email, password)
.then((result) => {
let user = result.user;
let userId = user.id;
let userEmail = email;
const toInsert = {
“_id”: userId,
“email”: userEmail
};
wixData.insert("Client", toInsert)
.then(() => {
wixLocation.to(`/Client/Register/${wixUsers.currentUser.id}`)
})
. **catch** ((err) => {
console.log(err)
})
})
. **catch** ((err) => {
console.log(err);
$w('#error').show()
$w('#have').show()
})
})
The above code directs to a dynamic page ‘/Client/Register/${wixUsers.currentUser.id}’ which has a form for the new user to add all their details to the ‘client’ collection: name, address, phone, plus many other details I require. This is all done with databinding, no code on that page and it all works fine.
I am not using the wixCRM contactInfo + custom fields because I need to be able to access all the data using code on other pages (not possible to access CRM data) - using a custom collection makes this possible - HOWEVER, I still want to use the wix CRM to contact my client…
The above method adds the client’s email to the CRM, but their their full name/address/phone/etc are only inserted into the ‘client’ collection. I can easily add these details in the CRM, but it has to be done manually for each user…
I am trying to write a backend afterUpdate function that will mirror the client’s name/address/phone/etc in the CRM whenever a user updates their data in the ‘client’ collection.
I am trying to do it like this in the data.js, but it returns an error code when submitting the update form and nothing changes in the CRM contact details (although the ‘client’ collection still updates fine):
import wixCRM from ‘wix-crm-backend’;
export function Client_afterUpdate(firstName, lastName, email, mobile) {
return wixCRM.createContact({
“firstName”: firstName,
“lastName”: lastName,
“emails”: [email],
“phones”: [mobile]
})
}
I have also tried doing it like this, but no difference:
export function Client_afterUpdate(item, context) {
let firstName = ${item.firstName}
let lastName = ${item.lastName}
let email = ${item.email}
let phone = ${item.mobile}
return wixCRM.createContact({
“firstName”: firstName,
“lastName”: lastName,
“emails”: [email],
“phones”: [phone]
})
}
I have been careful to add the hook from the database app, rather than just write the code straight into the data.js file.
I am using an _afterUpdate hook as the ‘client’ item is created with only an email, then updated with the form to include name/phone/address/etc.
I also have a simple _beforeUpdate hook that successfully inserts a client name into a field based on first and last names that the user has submitted:
export function Client_beforeUpdate(item, context) {
item.client = ${item.firstName} ${item.lastName}
;
return item;
}
Perhaps I should be trying to update the CRM contactInfo inside this _beforeUpdate hook instead??
Please can anyone advise on how to solve my problem??!!
Many Thanks!