Search.../

getContact( )

Retrieves a contact.

Description

The getContact() function returns a Promise that resolves when the contact is found.

Getting Merged Contacts

When a source contact is merged with a target contact, the source contact is deleted. When calling getContact() for a merged contact, you can use the source or target contact ID. In both cases, the target contact is returned.

This is supported only when calling getContact(), and only for merged contacts. Deleted source contact IDs are not supported on any other function.

Notes:

  • This function replaces the deprecated wixCrmBackend.getContactById(). 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.
  • Only visitors with Manage Contacts permissions can retrieve contacts. You can override the permissions by setting the suppressAuth option to true.

Syntax

function getContact(contactId: string, [options: AuthOptions]): Promise<Contact>

getContact Parameters

NAME
TYPE
DESCRIPTION
contactId
string

ID of the contact to retrieve.

options
Optional
AuthOptions

Authorization options.

Returns

Fulfilled - The requested 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?

Get a contact

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts } from 'wix-crm-backend';
3
4export const myGetContactFunction = webMethod(Permissions.Anyone, () => {
5 const contactId = "bc0ae72b-3285-485b-b0ad-c32c769a4daf";
6 const options = {
7 suppressAuth: false
8 };
9
10 return contacts.getContact(contactId, options)
11 .then((contact) => {
12 return contact;
13 })
14 .catch((error) => {
15 console.error(error);
16 });
17});
18
19/* Promise resolves to:
20 * {
21 * "_id": "bc0ae72b-3285-485b-b0ad-c32c769a4daf",
22 * "_createdDate": "2021-03-30T13:12:39.650Z",
23 * "_updatedDate": "2021-03-30T13:12:39.650Z",
24 * "revision": 0,
25 * "info": {
26 * "name": {
27 * "first": "Gene",
28 * "last": "Lopez"
29 * },
30 * "birthdate": "1981-11-02",
31 * "company": "Borer and Sons, Attorneys at Law",
32 * "jobTitle": "Senior Staff Attorney",
33 * "labelKeys": [
34 * "custom.white-glove-treatment",
35 * "contacts.contacted-me",
36 * "custom.new-lead"
37 * ],
38 * "locale": "en-us",
39 * "emails": [
40 * {
41 * "_id": "5bdcce4a-37c2-46ed-b49c-d562c6e3c4ce",
42 * "tag": "HOME",
43 * "email": "gene.lopez.at.home@example.com",
44 * "primary": true
45 * },
46 * {
47 * "_id": "78e5f398-e148-448d-b490-7c0b7d2ab336",
48 * "tag": "WORK",
49 * "email": "gene.lopez@example.com",
50 * "primary": false
51 * }
52 * ],
53 * "phones": [
54 * {
55 * "_id": "820e4640-ffe0-4980-a097-62a715e73135",
56 * "tag": "MOBILE",
57 * "countryCode": "US",
58 * "phone": "(722)-138-3099",
59 * "primary": true
60 * },
61 * {
62 * "_id": "8506549e-e4f8-42f6-b6fc-9db155b582ef",
63 * "tag": "HOME",
64 * "countryCode": "US",
65 * "phone": "(704)-454-1233",
66 * "e164Phone": "+17044541233",
67 * "primary": false
68 * }
69 * ],
70 * "addresses": [
71 * {
72 * "address": {
73 * "formatted": "9834 Bollinger Rd\nEl Cajon, WY 97766\nUS",
74 * "location": {
75 * "latitude": 84.1048,
76 * "longitude": -116.8836
77 * },
78 * "city": "El Cajon",
79 * "subdivision": "US-WY",
80 * "country": "US",
81 * "postalCode": "97766",
82 * "streetAddress": {
83 * "name": "Bollinger Rd",
84 * "number": "9834",
85 * "apt": ""
86 * }
87 * },
88 * "_id": "8532051f-91f2-42d9-9a97-9f2c39e64f7a",
89 * "tag": "HOME"
90 * }
91 * ],
92 * "profilePicture": "https://randomuser.me/api/portraits/men/0.jpg",
93 * "extendedFields": {
94 * "contacts.displayByLastName": "Lopez Gene",
95 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",
96 * "emailSubscriptions.subscriptionStatus": "NOT_SET",
97 * "custom.event-we-met-at": "LegalBigData",
98 * "emailSubscriptions.effectiveEmail": "gene.lopez.at.home@example.com",
99 * "contacts.displayByFirstName": "Gene Lopez"
100 * }
101 * },
102 * "lastActivity": {
103 * "activityDate": "2021-03-30T13:12:39.649Z",
104 * "activityType": "CONTACT_CREATED"
105 * },
106 * "primaryInfo": {
107 * "email": "gene.lopez.at.home@example.com",
108 * "phone": "(722)-138-3099"
109 * },
110 * "source": {
111 * "sourceType": "OTHER"
112 * }
113 * }
114 */
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
Get a merged source contact

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts } from 'wix-crm-backend';
3
4/* Sample contactId value:
5 * 'c8e08afd-deac-40f7-b4c1-b42409d35df7'
6 */
7
8export const myGetContactFunction = webMethod(Permissions.Anyone, async (contactId) => {
9 try {
10 const retrievedContact = await contacts.getContact(contactId);
11 const retrievedContactId = retrievedContact._id;
12
13 if (retrievedContactId !== contactId) {
14 console.log(`Merged target contact ${retrievedContactId} returned. The requested contact was deleted in the merge.`);
15 }
16
17 return retrievedContact;
18
19 } catch (error) {
20 console.error(error);
21 // Handle the error
22 }
23
24});
25
26/* Promise resolves to:
27 * {
28 * "_id": "49c5d809-6bc8-40b1-86e7-4bf3f6d17adb",
29 * "_createdDate": "2022-02-02T21:41:03.313Z",
30 * "_updatedDate": "2022-02-02T21:52:00.048Z",
31 * "revision": 6,
32 * "lastActivity": {
33 * "activityDate": "2022-02-02T21:51:59.952Z",
34 * "activityType": "CONTACT_MERGED"
35 * },
36 * "primaryInfo": {
37 * "email": "target.contact@example.com",
38 * "phone": "62183848"
39 * },
40 * "info": {
41 * "name": {
42 * "first": "Updated Target"
43 * },
44 * "emails": [
45 * {
46 * "_id": "81109065-7b9e-4b99-b146-b523eb5338ca",
47 * "tag": "HOME",
48 * "email": "target.contact@example.com",
49 * "primary": true
50 * },
51 * {
52 * "_id": "cd9ea33c-4358-457f-bd99-4384cf5f83c2",
53 * "tag": "WORK",
54 * "email": "source.contact@example.com",
55 * "primary": false
56 * }
57 * ],
58 * "addresses": [
59 * {
60 * "address": {
61 * "formatted": "4197 Existing Street Address\nHovedstaden Klitmøller\nDenmark",
62 * "city": "Klitmøller",
63 * "country": "DK",
64 * "postalCode": "Hovedstaden",
65 * "addressLine1": "4197 Existing Street Address"
66 * },
67 * "_id": "a3455bd0-76af-427c-9adc-8d384193f347",
68 * "tag": "HOME"
69 * },
70 * {
71 * "address": {
72 * "formatted": "9278 Source Street\n31562 København S\nDenmark",
73 * "city": "København S",
74 * "subdivision": "DK-82",
75 * "country": "DK",
76 * "postalCode": "31562",
77 * "addressLine1": "9278 Source Street"
78 * },
79 * "_id": "c3bb555c-a543-4e0a-86e3-341ced30666c",
80 * "tag": "WORK"
81 * }
82 * ],
83 * "phones": [
84 * {
85 * "_id": "5a53acb4-fd19-411e-93f3-3bd7ca8f9df8",
86 * "tag": "HOME",
87 * "countryCode": "DK",
88 * "phone": "62183848",
89 * "e164Phone": "+4562183848",
90 * "formattedPhone": "+45 62183848",
91 * "primary": true
92 * },
93 * {
94 * "_id": "b98e5caf-95d6-4fab-b80c-267431b82344",
95 * "tag": "WORK",
96 * "countryCode": "DK",
97 * "phone": "64498094",
98 * "e164Phone": "+4564498094",
99 * "formattedPhone": "+45 64498094",
100 * "primary": false
101 * }
102 * ],
103 * "labelKeys": [
104 * "contacts.contacted-me"
105 * ],
106 * "extendedFields": {
107 * "contacts.displayByLastName": "Updated Target",
108 * "invoices.vatId": "",
109 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",
110 * "emailSubscriptions.subscriptionStatus": "NOT_SET",
111 * "emailSubscriptions.effectiveEmail": "source.contact@example.com",
112 * "contacts.displayByFirstName": "Updated Target"
113 * },
114 * "profilePicture": "https://img-wixmp-8be454c954980f083caba37c.wixmp.com/sites/7b940519-404c-4972-9f03-9a430b68d52c/49c5d809-6bc8-40b1-86e7-4bf3f6d17adb/ecb8b31b-845c-4bc6-b868-73df78a92313-avatar-v2.jpeg::fil:100_100"
115 * },
116 * "source": {
117 * "sourceType": "ADMIN"
118 * }
119 */