How to use an _afterUpdate hook to update contactInfo in CRM when updating collection data??

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!

Hi Jamie:

There could be several issues that you are encountering in your backend code which are sometimes tough to debug. To start with it would be helpful to have a better idea about what you are trying to achieve then helping you solve the problem will be easier.

Is the focus of this question …

HOWEVER, I still want to use the wix CRM to contact my client… ?
So you want to send a triggered email to your client is that right? You don’t need any information in the CRM to do this you can simply use triggered emails and the user id that you saved from the register result.

Check out the triggered email documentation Getting Started with Triggered Emails

If I have misinterpreted your objective then can you provide a clearer objective? What you are trying to do may not be necessary :-).

Now a quick note on what you are trying to do. As you correctly figured out the wix-crm api is one way. But does some email checking to prevent duplicate record creation etc. So if you happen to have a Contact Record in your CRM with the email and you don’t present the right information in the create call then it will likely fail. Anyway the api is not well designed at this point and will give you problems.

If you want to capture more information than the email and password and save it to the CRM then I suggest that you do it all as part of register and try to limit you CRM interaction through wix-users if possible. You can add CRM fields to the register call as registration options:

function register(email: String, password: String, [options: RegistrationOptions]): Promise

https://www.wix.com/code/reference/wix-users.html#register


let email = // the user's email addresses 
let password = // the user's password 
let firstName = // the user's first name 
let lastName = // the user's last name 
let phone = // the user's phone number 
let nickname = // the user's nickname  

wixUsers.register(email, password, {
     contactInfo: { "firstName": firstName,
                    "lastName": lastName,
                    "phone": phone,
                    "nickname": nickname
                   }
})
.then( (result) => {
   let resultStatus = result.status;
} );
   

The contactInfo object is described here:

Steve