> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: findOrCreateLabel(displayName: string, options: FindOrCreateLabelOptions) # Method package: wixCrmV2 # Method menu location: wixCrmV2 --> labels --> findOrCreateLabel # Method Link: https://dev.wix.com/docs/velo/apis/wix-crm-v2/labels/find-or-create-label.md # Method Description: Retrieves a label with a specified name, or creates one if it doesn't exist. Successful calls to this method always return a label, which can be specified in subsequent calls. For example, in the Contacts API, Label Contact and Unlabel Contact calls will fail if you include a non-existent label. To ensure successful calls, you can call this method first, and then use the response in the Label Contact and Unlabel Contact calls. To find an existing label without potentially creating a new one, call Get Label or Query Labels. # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Create a label (dashboard page code) ```javascript import { labels } from 'wix-crm.v2'; /* Sample displayName value: 'At Risk' */ export async function myCreateLabelFunction(displayName) { try { const newLabel = await labels.findOrCreateLabel(displayName); console.log('Successfully created a new label:', newLabel); return newLabel; } catch (error) { console.log(error); // Handle the error } } /* Promise resolves to: * { * "label": { * "namespace": "custom", * "namespaceDisplayName": "Labels", * "key": "custom.at-risk", * "displayName": "At Risk", * "labelType": "USER_DEFINED", * "legacyId": "65bd6a68-e10e-4831-8d92-c90e75be1570", * "_createdDate": "2023-12-25T08:38:36.138Z", * "_updatedDate": "2023-12-25T08:38:36.138Z" * }, * "newLabel": true * } */ ``` ## Create a label (export from backend code) ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { labels } from 'wix-crm.v2'; import { elevate } from 'wix-auth'; /* Sample displayName value: 'At Risk' */ export const myCreateLabelFunction = webMethod(Permissions.Anyone, async (displayName) => { try { const elevatedCreateLabel = elevate(labels.findOrCreateLabel); const newLabel = await elevatedCreateLabel(displayName); console.log('Successfully created a new label:', newLabel); return newLabel; } catch (error) { console.log(error); // Handle the error } }); /* Promise resolves to: * { * "label": { * "namespace": "custom", * "namespaceDisplayName": "Labels", * "key": "custom.at-risk", * "displayName": "At Risk", * "labelType": "USER_DEFINED", * "legacyId": "65bd6a68-e10e-4831-8d92-c90e75be1570", * "_createdDate": "2023-12-25T08:38:36.138Z", * "_updatedDate": "2023-12-25T08:38:36.138Z" * }, * "newLabel": true * } */ ``` ## Find a label ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { labels } from 'wix-crm.v2'; import { elevate } from 'wix-auth'; /* Sample displayName value: 'Active Customer' */ export const myFindLabelFunction = webMethod(Permissions.Anyone, async (displayName) => { try { const elevatedFindLabel = elevate(labels.findOrCreateLabel); const label = await elevatedFindLabel(displayName); console.log('This label already exists:', label); return label; } catch (error) { console.log(error); // Handle the error } }); /* Promise resolves to: * { * "label": { * "namespace": "custom", * "namespaceDisplayName": "Labels", * "key": "custom.active-customer", * "displayName": "Active Customer", * "labelType": "USER_DEFINED", * "legacyId": "74f1e5c6-d9d5-4485-b272-13081ea35f38", * "_createdDate": "2023-12-25T06:13:21.000Z", * "_updatedDate": "2023-12-25T06:13:21.000Z" * }, * "newLabel": false * } */ ``` ## Create a new label and add it to a contact ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { contacts, labels } from 'wix-crm.v2'; import { elevate } from 'wix-auth'; /* Sample contactId value: '2712ae1d-3f64-46c2-ac3a-94a6c2e29847' * * Sample displayName value: 'Supplier' */ export const addNewLabelToContact = webMethod(Permissions.Anyone, async (displayName, contactId) => { try { const elevatedCreateLabel = elevate(labels.findOrCreateLabel); const elevatedLabelContact = elevate(contacts.labelContact); const newLabel = await elevatedCreateLabel(displayName); const labelKeys = [newLabel.label.key]; // Retrieve the new label's key if (newLabel.newLabel) { // Label contact const contact = await elevatedLabelContact(contactId, labelKeys); const contactLabels = contact.contact.info.labelKeys.items; return contactLabels; } else { return "Label was not added to contact."; } } catch (error) { console.log(error); // Handle the error } }); /* Promise resolves to: * [ * "custom.contact", * "custom.supplier" * ] */ ``` ---