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 into 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()
on merged contacts. Previously deleted source contact IDs can't be passed to any other function.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function getContact(_id: string, options: GetContactOptions): Promise<Contact>
getContact Parameters
NAME
TYPE
DESCRIPTION
ID of the contact to retrieve.
Get contact options.
Returns
The requested contact.
Return Type:
NAME
TYPE
DESCRIPTION
Date and time the contact was created.
Contact ID.
Date and time the contact was last updated.
Contact's details.
Details about the contact's last action in the site.
Contact's primary phone and email.
This property reflects the email address in contactInfo.emails
where primary
is true
.
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.
Was this helpful?
1import { contacts } from 'wix-crm.v2';23/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847' */45export async function myGetContactFunction(id) {67 try {8 const contact = await contacts.getContact(id);9 const contactId = contact._id;10 const contactName = contact.info.name.first;1112 return contact;13 } catch (error) {14 console.log(error);15 // Handle the error16 }17}1819/* Promise resolves to:20 * {21 * "revision": 1,22 * "source": {23 * "sourceType": "WIX_CODE",24 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"25 * },26 * "lastActivity": {27 * "activityDate": "2023-12-24T11:50:46.048Z",28 * "activityType": "CONTACT_CREATED"29 * },30 * "primaryInfo": {31 * "email": "johndoe1@example.com",32 * "phone": "6465676359"33 * },34 * "info": {35 * "name": {36 * "first": "John",37 * "last": "Doe"38 * },39 * "emails": {40 * "items": [41 * {42 * "tag": "UNTAGGED",43 * "email": "johndoe1@example.com",44 * "primary": true,45 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"46 * },47 * ]48 * },49 * "phones": {50 * "items": [51 * {52 * "tag": "MOBILE",53 * "countryCode": "US",54 * "phone": "646-567-6359",55 * "e164Phone": "+16465676359",56 * "primary": true,57 * "_id": "ed1d7da0-b4a1-4164-81b7-c49dedc25dd7"58 * }59 * ]60 * },61 * "addresses": {62 * "items": [63 * {64 * "tag": "HOME",65 * "address": {66 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",67 * "addressLine1": "3 Park Ave",68 * "city": "NY",69 * "subdivision": "US-NY",70 * "country": "US",71 * "postalCode": "10010"72 * },73 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"74 * }75 * ]76 * },77 * "company": "Wix",78 * "jobTitle": "Writer",79 * "birthdate": "1995-04-25",80 * "locale": "en-us",81 * "labelKeys": {82 * "items": [83 * "custom.contact"84 * ]85 * },86 * "extendedFields": {87 * "items": {88 * "contacts.displayByLastName": "Doe John",89 * "invoices.vatId": "",90 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",91 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",92 * "contacts.displayByFirstName": "John Doe",93 * "custom.nickname": "Jonny"94 * }95 * }96 * },97 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",98 * "_createdDate": "2023-12-24T11:50:46.049Z",99 * "_updatedDate": "2023-12-24T12:50:46.047Z"100 * }101 */102
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847' */67export const myGetContactFunction = webMethod(Permissions.Anyone, async (id) => {89 try {10 const elevatedGetContact = elevate(contacts.getContact);11 const contact = await elevatedGetContact(id);12 const contactId = contact._id;13 const contactName = contact.info.name.first;1415 return contact;16 } catch (error) {17 console.log(error);18 // Handle the error19 }20});2122/* Promise resolves to:23 * {24 * "revision": 1,25 * "source": {26 * "sourceType": "WIX_CODE",27 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"28 * },29 * "lastActivity": {30 * "activityDate": "2023-12-24T11:50:46.048Z",31 * "activityType": "CONTACT_CREATED"32 * },33 * "primaryInfo": {34 * "email": "johndoe1@example.com",35 * "phone": "6465676359"36 * },37 * "info": {38 * "name": {39 * "first": "John",40 * "last": "Doe"41 * },42 * "emails": {43 * "items": [44 * {45 * "tag": "UNTAGGED",46 * "email": "johndoe1@example.com",47 * "primary": true,48 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"49 * },50 * ]51 * },52 * "phones": {53 * "items": [54 * {55 * "tag": "MOBILE",56 * "countryCode": "US",57 * "phone": "646-567-6359",58 * "e164Phone": "+16465676359",59 * "primary": true,60 * "_id": "ed1d7da0-b4a1-4164-81b7-c49dedc25dd7"61 * }62 * ]63 * },64 * "addresses": {65 * "items": [66 * {67 * "tag": "HOME",68 * "address": {69 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",70 * "addressLine1": "3 Park Ave",71 * "city": "NY",72 * "subdivision": "US-NY",73 * "country": "US",74 * "postalCode": "10010"75 * },76 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"77 * }78 * ]79 * },80 * "company": "Wix",81 * "jobTitle": "Writer",82 * "birthdate": "1995-04-25",83 * "locale": "en-us",84 * "labelKeys": {85 * "items": [86 * "custom.contact"87 * ]88 * },89 * "extendedFields": {90 * "items": {91 * "contacts.displayByLastName": "Doe John",92 * "invoices.vatId": "",93 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",94 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",95 * "contacts.displayByFirstName": "John Doe",96 * "custom.nickname": "Jonny"97 * }98 * }99 * },100 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",101 * "_createdDate": "2023-12-24T11:50:46.049Z",102 * "_updatedDate": "2023-12-24T12:50:46.047Z"103 * }104 */
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'6 *7 * Sample options value:8 * {9 * fields: ['createdDate'],10 * fieldsets: ['BASIC']11 * }12 */1314export const myGetContactFunction = webMethod(Permissions.Anyone, async (id) => {15 try {16 const elevatedGetContact = elevate(contacts.getContact);17 const contact = await elevatedGetContact(id);18 const contactId = contact._id;19 const contactName = contact.info.name.first;2021 return contact;22 } catch (error) {23 console.log(error);24 // Handle the error25 }26});2728/*29 * Promise resolves to:30 * {31 * "revision": 1,32 * "primaryInfo": {33 * "email": "johndoe1@example.com",34 * "phone": "6465676359"35 * },36 * "info": {37 * "name": {38 * "first": "John",39 * "last": "Doe"40 * }41 * },42 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",43 * "_createdDate": "2023-12-24T11:50:46.049Z",44 * "_updatedDate": "2023-12-24T12:50:46.049Z"45 * }46 */
The page code passes a contact ID with updated contact information to the backend overwriteContactInfo()
function. The overwriteContactInfo()
function retrieves the contact, extracts the revision number, then passes it to contacts.updateContact()
. This allows the contact to be updated without needing to specify the revision. >Note: This code example always retrieves the latest revision before updating a contact.
1/**********************************2 * Backend code - contacts.web.js *3 **********************************/45import { Permissions, webMethod } from 'wix-web-module';6import { contacts } from 'wix-crm.v2';7import { elevate } from 'wix-auth';89export const overwriteContactInfo = webMethod(Permissions.Anyone, async (contactId, updatedContactInfo) => {1011 try {12 // Retrieve the contact13 const elevateGetContact = elevate(contacts.getContact);14 const contact = await elevateGetContact(contactId);15 // Extract revision number16 const revisionNumber = { revision: contact.revision };17 // Update the contact18 const elevateUpdateContact = elevate(contacts.updateContact);19 const updatedContact = await elevateUpdateContact(contactId, updatedContactInfo, revisionNumber);2021 return updatedContact;22 } catch (error) {23 console.log(error);24 // Handle the error25 }26});272829/*************30 * Page code *31 *************/3233import { overwriteContactInfo } from 'backend/contacts.web';3435$w.onReady(async function () {36 const contactId = "2712ae1d-3f64-46c2-ac3a-94a6c2e29847";37 const updatedContactInfo = {38 name: {39 first: "John",40 last: "Mitchell"41 }42 };43 async function updateContactName(){4445 try {46 const updatedContact = await overwriteContactInfo(contactId, updatedContactInfo);4748 return updatedContact;49 } catch(error){50 console.log(error);51 }52 }53 const result = await updateContactName();54 const first = result.contact.info.name.first;55 const last = result.contact.info.name.last;56 console.log(`Updated contact name: ${first} ${last}`);57});58