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

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