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 { contacts } from 'wix-crm-backend';
2
3export function myUpdateContactFunction() {
4 const contactIdentifiers = {
5 contactId: "0677ef55-cf20-4f68-989a-f31d3649eb72",
6 revision: 6
7 };
8 const contactInfo = {
9 name: {
10 first: "Annie",
11 last: "New Name"
12 },
13 extendedFields: {
14 "custom.event-we-met-at": "Cloud Computing MegaCon"
15 }
16 };
17 const options = {
18 allowDuplicates: false,
19 suppressAuth: false
20 };
21
22 return contacts.updateContact(contactIdentifiers, contactInfo, options)
23 .then((updatedContact) => {
24 return updatedContact;
25 })
26 .catch((error) => {
27 console.error(error);
28 });
29}
30
31/* Promise resolves to:
32 * {
33 * "_id": "0677ef55-cf20-4f68-989a-f31d3649eb72",
34 * "_createdDate": "2021-03-31T20:24:48.393Z",
35 * "_updatedDate": "2021-04-07T20:37:48.588Z",
36 * "revision": 7,
37 * "info": {
38 * "name": {
39 * "first": "Annie",
40 * "last": "New Name"
41 * },
42 * "extendedFields": {
43 * "custom.event-we-met-at": "Cloud Computing MegaCon",
44 * "contacts.displayByFirstName": "Annie New Name",
45 * "contacts.displayByLastName": "New Name Annie"
46 * }
47 * },
48 * "lastActivity": {
49 * "activityDate": "2021-03-31T20:24:48.393Z",
50 * "activityType": "CONTACT_CREATED"
51 * },
52 * "source": {
53 * "appId": "manual",
54 * "sourceType": "ADMIN"
55 * }
56 * }
57 */
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.jsw *
3 *******************************/
4
5import { contacts } from 'wix-crm-backend';
6
7export async function overwriteContactInfo(contactId, updatedContactInfo) {
8
9 // Get the contact's last revision number
10 const myContact = await contacts.getContact(contactId);
11 const contactIdentifiers = {
12 contactId: contactId,
13 revision: myContact.revision
14 };
15 const options = {
16 allowDuplicates: false,
17 suppressAuth: false
18 };
19
20 return await contacts.updateContact(contactIdentifiers, updatedContactInfo, options);
21}
22
23/*************
24 * Page code *
25 *************/
26
27 import { overwriteContactInfo } from 'backend/contacts';
28
29 // ...
30
31 const contactId = "0677ef55-cf20-4f68-989a-f31d3649eb72";
32
33 const updatedContactInfo = {
34 name: {
35 first: "Annie",
36 last: "New Name"
37 }
38 };
39
40 overwriteContactInfo(contactId, updatedContactInfo)
41 .then((updatedContact) => {
42 return updatedContact;
43 })
44 .catch((error) => {
45 console.error(error);
46 });
47
48/* Updated contact resolves to:
49 * {
50 * "_id": "0677ef55-cf20-4f68-989a-f31d3649eb72",
51 * "_createdDate": "2021-03-31T20:24:48.393Z",
52 * "_updatedDate": "2021-03-31T20:29:14.519Z",
53 * "revision": 4,
54 * "info": {
55 * "name": {
56 * "first": "Annie",
57 * "last": "New Name"
58 * },
59 * "extendedFields": {
60 * "contacts.displayByFirstName": "Annie New Name",
61 * "contacts.displayByLastName": "New Name Annie"
62 * }
63 * },
64 * "lastActivity": {
65 * "activityDate": "2021-03-31T20:24:48.393Z",
66 * "activityType": "CONTACT_CREATED"
67 * },
68 * "source": {
69 * "appId": "manual",
70 * "sourceType": "ADMIN"
71 * }
72 * }
73 */