updateContact( )
Updates a contact's properties.
Description
The updateContact()
function returns a Promise that resolves
when the specified contact's information is updated.
Note: This function replaces the deprecated
wixCrmBackend.updateContact()
. The deprecated function will continue to work, but it will not receive updates. To keep any existing code compatible with future changes, see the migration instructions.
Each time the contact is updated,
revision
increments by 1.
The existing revision
must be included when updating the contact.
This ensures you're working with the latest contact information,
and it prevents unintended overwrites.
Note: Only visitors with Manage Contacts permissions can update contacts. You can override the permissions by setting the
suppressAuth
option totrue
.
Syntax
function updateContact(identifiers: Identifiers, contactInfo: ContactInfo, [options: Options]): Promise<Contact>
updateContact Parameters
NAME
TYPE
DESCRIPTION
Contact ID and revision number.
Contact info.
Contact update options.
Returns
Fulfilled - Updated contact.
Return Type:
NAME
TYPE
DESCRIPTION
Contact ID.
Revision number, which increments by 1 each time the contact is updated.
To prevent conflicting changes,
the existing revision
must be used when updating a contact.
Details about the contact's source.
Date and time the contact was created.
Date and time the contact was last updated.
Details about the contact's last action in the site.
Contact's primary phone and email.
Contact's details.
Was this helpful?
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm-backend';34export const myUpdateContactFunction = webMethod(Permissions.Anyone, () => {5 const contactIdentifiers = {6 contactId: "0677ef55-cf20-4f68-989a-f31d3649eb72",7 revision: 68 };9 const contactInfo = {10 name: {11 first: "Annie",12 last: "New Name"13 },14 extendedFields: {15 "custom.event-we-met-at": "Cloud Computing MegaCon"16 }17 };18 const options = {19 allowDuplicates: false,20 suppressAuth: false21 };2223 return contacts.updateContact(contactIdentifiers, contactInfo, options)24 .then((updatedContact) => {25 return updatedContact;26 })27 .catch((error) => {28 console.error(error);29 });30});3132/* Promise resolves to:33 * {34 * "_id": "0677ef55-cf20-4f68-989a-f31d3649eb72",35 * "_createdDate": "2021-03-31T20:24:48.393Z",36 * "_updatedDate": "2021-04-07T20:37:48.588Z",37 * "revision": 7,38 * "info": {39 * "name": {40 * "first": "Annie",41 * "last": "New Name"42 * },43 * "extendedFields": {44 * "custom.event-we-met-at": "Cloud Computing MegaCon",45 * "contacts.displayByFirstName": "Annie New Name",46 * "contacts.displayByLastName": "New Name Annie"47 * }48 * },49 * "lastActivity": {50 * "activityDate": "2021-03-31T20:24:48.393Z",51 * "activityType": "CONTACT_CREATED"52 * },53 * "source": {54 * "appId": "manual",55 * "sourceType": "ADMIN"56 * }57 * }58 */
The page code passes a contact ID and updated contact information
to the backend overwriteContactInfo()
function.
overwriteContactInfo()
then retrieves the contact,
extracts the revision number, and passes it to contacts.updateContact()
.
This ensures that the contact is always updated
without needing to specify the revision.
This can be used when it's not important that some contact information could be overwritten.
1/*******************************2 * Backend code - contacts.web.js *3 *******************************/45import { Permissions, webMethod } from 'wix-web-module';6import { contacts } from 'wix-crm-backend';78export const overwriteContactInfo = webMethod(Permissions.Anyone, async (contactId, updatedContactInfo) => {910 // Get the contact's last revision number11 const myContact = await contacts.getContact(contactId);12 const contactIdentifiers = {13 contactId: contactId,14 revision: myContact.revision15 };16 const options = {17 allowDuplicates: false,18 suppressAuth: false19 };2021 return await contacts.updateContact(contactIdentifiers, updatedContactInfo, options);22});2324/*************25 * Page code *26 *************/2728import { overwriteContactInfo } from 'backend/contacts.web';2930// ...3132const contactId = "0677ef55-cf20-4f68-989a-f31d3649eb72";3334const updatedContactInfo = {35 name: {36 first: "Annie",37 last: "New Name"38 }39};4041overwriteContactInfo(contactId, updatedContactInfo)42 .then((updatedContact) => {43 return updatedContact;44 })45 .catch((error) => {46 console.error(error);47 });4849/* Updated contact resolves to:50 * {51 * "_id": "0677ef55-cf20-4f68-989a-f31d3649eb72",52 * "_createdDate": "2021-03-31T20:24:48.393Z",53 * "_updatedDate": "2021-03-31T20:29:14.519Z",54 * "revision": 4,55 * "info": {56 * "name": {57 * "first": "Annie",58 * "last": "New Name"59 * },60 * "extendedFields": {61 * "contacts.displayByFirstName": "Annie New Name",62 * "contacts.displayByLastName": "New Name Annie"63 * }64 * },65 * "lastActivity": {66 * "activityDate": "2021-03-31T20:24:48.393Z",67 * "activityType": "CONTACT_CREATED"68 * },69 * "source": {70 * "appId": "manual",71 * "sourceType": "ADMIN"72 * }73 * }74 */75