Search.../

findOrCreateLabel( )

Retrieves a label with a given name, or creates one if it doesn't exist.

Description

The findOrCreateLabel() function returns a Promise that resolves when the specified label is found or created.

Successful calls to findOrCreateLabel() always return a label, which can be passed to subsequent function calls.

To find an existing label without potentially creating a new one, use getLabel() or queryLabels().

Note: Only visitors with Manage Contacts permissions can find or create labels. You can override the permissions by setting the suppressAuth option to true.

Syntax

function findOrCreateLabel(displayName: string, [options: AuthOptions]): Promise<FoundOrCreatedLabel>

findOrCreateLabel Parameters

NAME
TYPE
DESCRIPTION
displayName
string

Label display name to retrieve or create.

If an existing label is an exact match for the specified display name, the existing label is returned. If not, a new label is created and returned.

options
Optional
AuthOptions

Authorization options.

Returns

Fulfilled - Label that was found or created.

Return Type:

Promise<FoundOrCreatedLabel>
NAME
TYPE
DESCRIPTION
label
Label

Label that was found or created.

newLabel
boolean

Indicates whether the label was just created or already existed.

If the label was just created, returns true. If it already existed, returns false.

Was this helpful?

Create a label

Copy Code
1import { contacts } from 'wix-crm-backend';
2
3export function myFindOrCreateLabelFunction() {
4 const displayName = "Active Customer";
5 const options = {
6 suppressAuth: false
7 };
8
9 return contacts.findOrCreateLabel(displayName, options)
10 .then((label) => {
11 return label;
12 })
13 .catch((error) => {
14 console.error(error);
15 });
16}
17
18/*
19 * Promise resolves to:
20 *
21 * {
22 * "label": {
23 * "_createdDate": "2021-01-20T00:31:41.452Z",
24 * "_updatedDate": "2021-01-20T00:31:41.452Z"
25 * "namespace": "custom",
26 * "key": "custom.active-customer",
27 * "displayName": "Active Customer",
28 * "labelType": "USER_DEFINED"
29 * },
30 * "newLabel": true
31 * }
32 */
Find a label

Copy Code
1import { contacts } from 'wix-crm-backend';
2
3export function myFindOrCreateLabelFunction() {
4 const displayName = "Active Customer";
5 const options = {
6 suppressAuth: false
7 };
8
9 return contacts.findOrCreateLabel(displayName, options)
10 .then((label) => {
11 return label;
12 })
13 .catch((error) => {
14 console.error(error);
15 });
16}
17
18/*
19 * Promise resolves to:
20 *
21 * {
22 * "label": {
23 * "_createdDate": "2021-01-20T00:31:41.452Z",
24 * "_updatedDate": "2021-01-20T00:31:41.452Z"
25 * "namespace": "custom",
26 * "key": "custom.active-customer",
27 * "displayName": "Active Customer",
28 * "labelType": "USER_DEFINED"
29 * },
30 * "newLabel": true
31 * }
32 */