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 returns 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()
.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function findOrCreateLabel(displayName: string, options: FindOrCreateLabelOptions): Promise<FindOrCreateLabelResponse>
findOrCreateLabel Parameters
NAME
TYPE
DESCRIPTION
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.
Language options.
Returns
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 { labels } from 'wix-crm.v2';23/* Sample displayName value: 'At Risk' */45export async function myCreateLabelFunction(displayName) {67 try {8 const newLabel = await labels.findOrCreateLabel(displayName);9 console.log('Successfully created a new label:', newLabel);1011 return newLabel;12 } catch (error) {13 console.log(error);14 // Handle the error15 }16}1718/* Promise resolves to:19 * {20 * "label": {21 * "namespace": "custom",22 * "namespaceDisplayName": "Labels",23 * "key": "custom.at-risk",24 * "displayName": "At Risk",25 * "labelType": "USER_DEFINED",26 * "legacyId": "65bd6a68-e10e-4831-8d92-c90e75be1570",27 * "_createdDate": "2023-12-25T08:38:36.138Z",28 * "_updatedDate": "2023-12-25T08:38:36.138Z"29 * },30 * "newLabel": true31 * }32 */
1import { Permissions, webMethod } from 'wix-web-module';2import { labels } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample displayName value: 'At Risk' */67export const myCreateLabelFunction = webMethod(Permissions.Anyone, async (displayName) => {89 try {10 const elevatedCreateLabel = elevate(labels.findOrCreateLabel);11 const newLabel = await elevatedCreateLabel(displayName);12 console.log('Successfully created a new label:', newLabel);1314 return newLabel;15 } catch (error) {16 console.log(error);17 // Handle the error18 }19});2021/* Promise resolves to:22 * {23 * "label": {24 * "namespace": "custom",25 * "namespaceDisplayName": "Labels",26 * "key": "custom.at-risk",27 * "displayName": "At Risk",28 * "labelType": "USER_DEFINED",29 * "legacyId": "65bd6a68-e10e-4831-8d92-c90e75be1570",30 * "_createdDate": "2023-12-25T08:38:36.138Z",31 * "_updatedDate": "2023-12-25T08:38:36.138Z"32 * },33 * "newLabel": true34 * }35 */36
1import { Permissions, webMethod } from 'wix-web-module';2import { labels } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample displayName value: 'Active Customer' */67export const myFindLabelFunction = webMethod(Permissions.Anyone, async (displayName) => {89 try {10 const elevatedFindLabel = elevate(labels.findOrCreateLabel);11 const label = await elevatedFindLabel(displayName);12 console.log('This label already exists:', label);1314 return label;15 } catch (error) {16 console.log(error);17 // Handle the error18 }19});202122/* Promise resolves to:23 * {24 * "label": {25 * "namespace": "custom",26 * "namespaceDisplayName": "Labels",27 * "key": "custom.active-customer",28 * "displayName": "Active Customer",29 * "labelType": "USER_DEFINED",30 * "legacyId": "74f1e5c6-d9d5-4485-b272-13081ea35f38",31 * "_createdDate": "2023-12-25T06:13:21.000Z",32 * "_updatedDate": "2023-12-25T06:13:21.000Z"33 * },34 * "newLabel": false35 * }36 */
This function creates a new label, gets the label's key, and then adds the label to the contact.
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts, labels } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847'6 *7 * Sample displayName value: 'Supplier'8 */910export const addNewLabelToContact = webMethod(Permissions.Anyone, async (displayName, contactId) => {1112 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 key18 if (newLabel.newLabel) {19 // Label contact20 const contact = await elevatedLabelContact(contactId, labelKeys);21 const contactLabels = contact.contact.info.labelKeys.items;2223 return contactLabels;24 }25 else {26 return "Label was not added to contact.";27 }28 } catch (error) {29 // Handle the error30 console.log(error);31 }32});3334/* Promise resolves to:35 * [36 * "custom.contact",37 * "custom.supplier"38 * ]39 */