Wix crm backend update giving errors

I’ve been trying to use the new wix crm backend code to update contact by passing the contact id for a logged in user.

https://www.wix.com/velo/reference/wix-crm-backend/contacts-obj/updatecontact

Console errors show as the following after trying out various things.

"jsonPayload":{

"message":"Error: Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information. "
}

//Another error showing up
jsonPayload":{
"message":"["message: ''\ndetails:\n applicationError:\n description: Forbidden\n code: FORBIDDEN\n data: {}"]"
}

Code is as follows below:

/*************
 * Page code *
 *************/
 import wixUsers from 'wix-users';
 import { overwriteContactInfo } from 'backend/contacts';
 
 const contactId = wixUsers.currentUser.id;
 console.log(contactId);

 const updatedContactInfo = {
   name: {
     first: "Annie",
     last: "New Name"
   }
 };

 overwriteContactInfo(contactId, updatedContactInfo)
  .then((updatedContact) => {
    return updatedContact;
    console.log(updatedContact);
  })
  .catch((error) => {
    console.error(error);
  });

/*******************************
 * Backend code - contacts.jsw *
 *******************************/

import { contacts } from 'wix-crm-backend';

export async function overwriteContactInfo(contactId, updatedContactInfo) {

  // Get the contact's last revision number
  const myContact = await contacts.getContact(contactId);
  const contactIdentifiers = {
    contactId: contactId,
    revision: myContact.revision
  };
  const options = {
    allowDuplicates: false,
  
    "suppressAuth": true,
    "suppressHooks": true
  };

  return await contacts.updateContact(contactIdentifiers, updatedContactInfo, options);
}

Can someone pls guide me as to what I am doing wrong???

Instead of this…

const myContact = await contacts.getContact(contactId);

…try this one…

const myContact = await contacts.getContact(contactId,{ suppressAuth:true});

Thanks @russian-dima . This worked to at least get that error out. Maybe the code api documentation can also be edited a bit?

@jaoshsethna
Yes! I had the same thought, when facing the same problem. No information about this in the API! You are right! And still nothing has been changed in the API-DOC.

@russian-dima Thanks! The team will update the API docs to include this.

Hi, @jaoshsethna and @russian-dima , I would like to know is your problem solved? Please help me in regard to create/update extended fields (custom fields). How can I update contact/extended/custom fields through this code? Kindly help It would be a life saving for my project.

You already opened your own post?

@jaoshsethna @russian-dima Hi, tech writer here :wave:

I just went through the contacts functions and added a note on suppressAuth in each description. The note gives a better idea of the permissions needed if you don’t use suppressAuth.

Thanks for the feedback!

1 Like