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 totrue
.
Syntax
function findOrCreateLabel(displayName: string, [options: AuthOptions]): Promise<FoundOrCreatedLabel>
findOrCreateLabel Parameters
NAME
TYPE
DESCRIPTION
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.
Authorization options.
Returns
Fulfilled - Label that was found or created.
Return Type:
NAME
TYPE
DESCRIPTION
Label that was found or created.
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?
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm-backend';34export const myFindOrCreateLabelFunction = webMethod(Permissions.Anyone, () => {5 const displayName = "Active Customer";6 const options = {7 suppressAuth: false8 };910 return contacts.findOrCreateLabel(displayName, options)11 .then((label) => {12 return label;13 })14 .catch((error) => {15 console.error(error);16 });17});1819/*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": true32 * }33 */
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm-backend';34export const myFindOrCreateLabelFunction = webMethod(Permissions.Anyone, () => {5 const displayName = "Active Customer";6 const options = {7 suppressAuth: false8 };910 return contacts.findOrCreateLabel(displayName, options)11 .then((label) => {12 return label;13 })14 .catch((error) => {15 console.error(error);16 });17});1819/*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": true32 * }33 */