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

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