Search.../

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 to true.

Syntax

function updateContact(identifiers: Identifiers, contactInfo: ContactInfo, [options: Options]): Promise<Contact>

updateContact Parameters

NAME
TYPE
DESCRIPTION
identifiers
Identifiers

Contact ID and revision number.

contactInfo
ContactInfo

Contact info.

options
Optional
Options

Contact update options.

Returns

Fulfilled - Updated contact.

Return Type:

Promise<Contact>
NAME
TYPE
DESCRIPTION
_id
string

Contact ID.

revision
number

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.

source
Source

Details about the contact's source.

_createdDate
Date

Date and time the contact was created.

_updatedDate
Date

Date and time the contact was last updated.

lastActivity
LastActivity

Details about the contact's last action in the site.

primaryInfo
PrimaryInfo

Contact's primary phone and email.

info
Info

Contact's details.

Was this helpful?

Update a contact

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts } from 'wix-crm-backend';
3
4export const myUpdateContactFunction = webMethod(Permissions.Anyone, () => {
5 const contactIdentifiers = {
6 contactId: "0677ef55-cf20-4f68-989a-f31d3649eb72",
7 revision: 6
8 };
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: false
21 };
22
23 return contacts.updateContact(contactIdentifiers, contactInfo, options)
24 .then((updatedContact) => {
25 return updatedContact;
26 })
27 .catch((error) => {
28 console.error(error);
29 });
30});
31
32/* 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 */
Get a contact's latest revision number, then update

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.

Copy Code
1/*******************************
2 * Backend code - contacts.web.js *
3 *******************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { contacts } from 'wix-crm-backend';
7
8export const overwriteContactInfo = webMethod(Permissions.Anyone, async (contactId, updatedContactInfo) => {
9
10 // Get the contact's last revision number
11 const myContact = await contacts.getContact(contactId);
12 const contactIdentifiers = {
13 contactId: contactId,
14 revision: myContact.revision
15 };
16 const options = {
17 allowDuplicates: false,
18 suppressAuth: false
19 };
20
21 return await contacts.updateContact(contactIdentifiers, updatedContactInfo, options);
22});
23
24/*************
25 * Page code *
26 *************/
27
28import { overwriteContactInfo } from 'backend/contacts.web';
29
30// ...
31
32const contactId = "0677ef55-cf20-4f68-989a-f31d3649eb72";
33
34const updatedContactInfo = {
35 name: {
36 first: "Annie",
37 last: "New Name"
38 }
39};
40
41overwriteContactInfo(contactId, updatedContactInfo)
42 .then((updatedContact) => {
43 return updatedContact;
44 })
45 .catch((error) => {
46 console.error(error);
47 });
48
49/* 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