Retrieves a label with a given name, or creates one if it doesn't exist.
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
.
function findOrCreateLabel(
displayName: string,
options: AuthOptions,
): Promise<FoundOrCreatedLabel>;
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.
import { Permissions, webMethod } from "wix-web-module";
import { contacts } from "wix-crm-backend";
export const myFindOrCreateLabelFunction = webMethod(Permissions.Anyone, () => {
const displayName = "Active Customer";
const options = {
suppressAuth: false,
};
return contacts
.findOrCreateLabel(displayName, options)
.then((label) => {
return label;
})
.catch((error) => {
console.error(error);
});
});
/*
* Promise resolves to:
*
* {
* "label": {
* "_createdDate": "2021-01-20T00:31:41.452Z",
* "_updatedDate": "2021-01-20T00:31:41.452Z"
* "namespace": "custom",
* "key": "custom.active-customer",
* "displayName": "Active Customer",
* "labelType": "USER_DEFINED"
* },
* "newLabel": true
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.