Search.../

labelContact( )

Adds labels to a contact.

Description

The labelContact() function returns a Promise that resolves when the specified labels are added to the contact.

  • To create new labels: Use findOrCreateLabel().
  • To find labels: Use findOrCreateLabel(), getLabel(), or queryLabels().
Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function labelContact(contactId: string, labelKeys: Array<string>): Promise<LabelContactResponse>

labelContact Parameters

NAME
TYPE
DESCRIPTION
contactId
string

ID of the contact to add labels to.

labelKeys
Array<
string
>

List of label keys to add to the contact.

Label keys must exist to be added to the contact. Contact labels can be created with the findOrCreateLabel() function. You can use the queryLabels() function to view the currently existing labels.

Returns

Updated contact.

Return Type:

Promise<
LabelContactResponse
>
NAME
TYPE
DESCRIPTION
contact
Contact

Updated contact.

Was this helpful?

Add a label to a contact (dashboard page code)

Copy Code
1import { contacts } from 'wix-crm.v2';
2
3/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'
4 *
5 * Sample labelKeys value:
6 * [
7 * 'custom.at-risk',
8 * 'custom.active-customer'
9 * ]
10 *
11 */
12
13export async function myLabelContactFunction(contactId, labelKeys) {
14
15 try {
16 const labeledContact = await contacts.labelContact(contactId, labelKeys);
17 console.log('Successfully added labels to contact');
18
19 return labeledContact;
20 } catch (error) {
21 console.log(error);
22 // Handle the error
23 }
24}
25
26/* Promise resolves to:
27 * {
28 * "contact": {
29 * "revision": 2,
30 * "source": {
31 * "sourceType": "WIX_CODE",
32 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
33 * },
34 * "lastActivity": {
35 * "activityDate": "2023-12-24T11:50:46.048Z",
36 * "activityType": "CONTACT_CREATED"
37 * },
38 * "primaryInfo": {
39 * "email": "johndoe1@example.com",
40 * "phone": "6465676359"
41 * },
42 * "info": {
43 * "name": {
44 * "first": "John",
45 * "last": "Doe"
46 * },
47 * "emails": {
48 * "items": [
49 * {
50 * "tag": "UNTAGGED",
51 * "email": "johndoe1@example.com",
52 * "primary": true,
53 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"
54 * }
55 * ]
56 * },
57 * "phones": {
58 * "items": [
59 * {
60 * "tag": "MOBILE",
61 * "countryCode": "US",
62 * "phone": "646-567-6359",
63 * "e164Phone": "+16465676359",
64 * "primary": true,
65 * "_id": "ed1d7da0-b4a1-4164-81b7-c49dedc25dd7"
66 * }
67 * ]
68 * },
69 * "addresses": {
70 * "items": [
71 * {
72 * "tag": "HOME",
73 * "address": {
74 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",
75 * "addressLine1": "3 Park Ave",
76 * "city": "NY",
77 * "subdivision": "US-NY",
78 * "country": "US",
79 * "postalCode": "10010"
80 * },
81 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"
82 * }
83 * ]
84 * },
85 * "company": "Wix",
86 * "jobTitle": "Writer",
87 * "birthdate": "1995-04-25",
88 * "locale": "en-us",
89 * "labelKeys": {
90 * "items": [
91 * "custom.contact",
92 * "custom.at-risk",
93 * "custom.active-customer"
94 * ]
95 * },
96 * "extendedFields": {
97 * "items": {
98 * "contacts.displayByLastName": "Doe John",
99 * "invoices.vatId": "",
100 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",
101 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",
102 * "contacts.displayByFirstName": "John Doe",
103 * "custom.nickname": "Jonny"
104 * }
105 * }
106 * },
107 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",
108 * "_createdDate": "2023-12-24T11:50:46.049Z",
109 * "_updatedDate": "2023-12-25T06:13:54.362Z"
110 * }
111 * }
112 *
113 */
114
Add a label to a contact (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts } from 'wix-crm.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'
6 *
7 * Sample labelKeys value:
8 * [
9 * 'custom.at-risk',
10 * 'custom.active-customer'
11 * ]
12 *
13 */
14
15export const myLabelContactFunction = webMethod(Permissions.Anyone, async (contactId, labelKeys) => {
16
17 try {
18 const elevatedLabelContact = elevate(contacts.labelContact);
19 const labeledContact = await elevatedLabelContact(contactId, labelKeys);
20 console.log('Successfully added labels to contact');
21
22 return labeledContact;
23 } catch (error) {
24 console.log(error);
25 // Handle the error
26 }
27});
28
29/* Promise resolves to:
30 * {
31 * "contact": {
32 * "revision": 2,
33 * "source": {
34 * "sourceType": "WIX_CODE",
35 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
36 * },
37 * "lastActivity": {
38 * "activityDate": "2023-12-24T11:50:46.048Z",
39 * "activityType": "CONTACT_CREATED"
40 * },
41 * "primaryInfo": {
42 * "email": "johndoe1@example.com",
43 * "phone": "6465676359"
44 * },
45 * "info": {
46 * "name": {
47 * "first": "John",
48 * "last": "Doe"
49 * },
50 * "emails": {
51 * "items": [
52 * {
53 * "tag": "UNTAGGED",
54 * "email": "johndoe1@example.com",
55 * "primary": true,
56 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"
57 * }
58 * ]
59 * },
60 * "phones": {
61 * "items": [
62 * {
63 * "tag": "MOBILE",
64 * "countryCode": "US",
65 * "phone": "646-567-6359",
66 * "e164Phone": "+16465676359",
67 * "primary": true,
68 * "_id": "ed1d7da0-b4a1-4164-81b7-c49dedc25dd7"
69 * }
70 * ]
71 * },
72 * "addresses": {
73 * "items": [
74 * {
75 * "tag": "HOME",
76 * "address": {
77 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",
78 * "addressLine1": "3 Park Ave",
79 * "city": "NY",
80 * "subdivision": "US-NY",
81 * "country": "US",
82 * "postalCode": "10010"
83 * },
84 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"
85 * }
86 * ]
87 * },
88 * "company": "Wix",
89 * "jobTitle": "Writer",
90 * "birthdate": "1995-04-25",
91 * "locale": "en-us",
92 * "labelKeys": {
93 * "items": [
94 * "custom.contact",
95 * "custom.at-risk",
96 * "custom.active-customer"
97 * ]
98 * },
99 * "extendedFields": {
100 * "items": {
101 * "contacts.displayByLastName": "Doe John",
102 * "invoices.vatId": "",
103 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",
104 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",
105 * "contacts.displayByFirstName": "John Doe",
106 * "custom.nickname": "Jonny"
107 * }
108 * }
109 * },
110 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",
111 * "_createdDate": "2023-12-24T11:50:46.049Z",
112 * "_updatedDate": "2023-12-25T06:13:54.362Z"
113 * }
114 *
115 */
116
Create a new label and add it to a contact

This function creates a new label, gets the label's key, and then adds the label to the contact.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { contacts, labels } from 'wix-crm.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'
6 *
7 * Sample displayName value: 'Supplier'
8 */
9
10export const addNewLabelToContact = webMethod(Permissions.Anyone, async (displayName, contactId) => {
11
12 try {
13 const elevatedCreateLabel = elevate(labels.findOrCreateLabel);
14 const elevatedLabelContact = elevate(contacts.labelContact);
15 const newLabel = await elevatedCreateLabel(displayName);
16 const labelKeys = [newLabel.label.key];
17 // Retrieve the new label's key
18 if (newLabel.newLabel) {
19 // Label contact
20 const contact = await elevatedLabelContact(contactId, labelKeys);
21 const contactLabels = contact.contact.info.labelKeys.items;
22
23 return contactLabels;
24 }
25 else {
26 return "Label was not added to contact.";
27 }
28 } catch (error) {
29 console.log(error);
30 // Handle the error
31 }
32});
33
34/* Promise resolves to:
35 * [
36 * "custom.contact",
37 * "custom.supplier"
38 * ]
39 */
40