Search.../

findOrCreateExtendedField( )

Developer Preview

Retrieves a custom field with a given name, or creates one if it doesn't exist.

Description

The findOrCreateExtendedField() function returns a Promise that resolves when the specified custom field is found or created.

Successful calls to findOrCreateExtendedField() always return an extended field, which can be passed to subsequent function calls.

To find an existing extended field without potentially creating a new one, use getExtendedField() or queryExtendedFields().

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function findOrCreateExtendedField(displayName: string, dataType: string): Promise<FindOrCreateExtendedFieldResponse>

findOrCreateExtendedField Parameters

NAME
TYPE
DESCRIPTION
displayName
string

Display name to find or create.

If an existing custom field is an exact match for the specified displayName, the existing field is returned. If not, a new field is created and returned.

dataType
string

Type of data the field holds. Ignored if an existing field is an exact match for the specified display name. One of:

  • "TEXT": Accepts strings.
  • "NUMBER": Accepts floats.
  • "DATE": Accepts dates formatted as YYYY-MM-DD.
  • "URL": Accepts strings. Prepends https:// if no protocol is included.

Returns

Extended field that was found or created.

Return Type:

Promise<
FindOrCreateExtendedFieldResponse
>
NAME
TYPE
DESCRIPTION
field
ExtendedField

Extended field that was found or created.

newField
boolean

Indicates whether the extended field was just created or already existed.

If the field was just created, returns true. If the field already existed, returns false.

Was this helpful?

Create an extended field (dashboard page code)

Copy Code
1import { extendedFields } from 'wix-crm.v2';
2
3/* Sample displayName value:
4 *
5 * 'Age'
6 *
7 * Sample dataType value:
8 *
9 * 'NUMBER'
10 */
11
12export async function myCreateExtendedFieldFunction(displayName, dataType) {
13
14 try {
15 const newExtendedField = await extendedFields.findOrCreateExtendedField(displayName, dataType);
16 console.log('Successfully created new extended field:', newExtendedField)
17
18 return newExtendedField;
19 } catch (error) {
20 console.log(error);
21 // Handle the error
22 }
23}
24
25/* Promise resolves to:
26 * {
27 * "field": {
28 * "namespace": "custom",
29 * "key": "custom.age",
30 * "displayName": "Age",
31 * "dataType": "NUMBER",
32 * "fieldType": "USER_DEFINED",
33 * "legacyId": "ed349d8c-b2bc-46a4-80d8-7632c6f50b00",
34 * "wixSearchColumn": "info_extendedFields_custom_double_27",
35 * "_createdDate": "2023-12-25T12:16:40.000Z",
36 * "_updatedDate": "2023-12-25T12:16:40.000Z"
37 * },
38 * "newField": false
39 * }
40 */
Create an extended field (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { extendedFields } from 'wix-crm.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample displayName value:
6 *
7 * 'Age'
8 *
9 * Sample dataType value:
10 *
11 * 'NUMBER'
12 */
13
14export const myCreateExtendedFieldFunction = webMethod(Permissions.Anyone, async (displayName, dataType) => {
15
16 try {
17 const elevatedCreateExtendedField = elevate(extendedFields.findOrCreateExtendedField);
18 const newExtendedField = await elevatedCreateExtendedField(displayName, dataType);
19 console.log('Successfully created new extended field:', newExtendedField)
20
21 return newExtendedField;
22 } catch (error) {
23 console.log(error);
24 // Handle the error
25 }
26});
27
28/* Promise resolves to:
29 * {
30 * "field": {
31 * "namespace": "custom",
32 * "key": "custom.age",
33 * "displayName": "Age",
34 * "dataType": "NUMBER",
35 * "fieldType": "USER_DEFINED",
36 * "legacyId": "ed349d8c-b2bc-46a4-80d8-7632c6f50b00",
37 * "wixSearchColumn": "info_extendedFields_custom_double_27",
38 * "_createdDate": "2023-12-25T12:16:40.000Z",
39 * "_updatedDate": "2023-12-25T12:16:40.000Z"
40 * },
41 * "newField": false
42 * }
43 */
44
Find an extended field

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { extendedFields } from 'wix-crm.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample displayName value:
6 *
7 * 'Nickname'
8 *
9 * Sample dataType value:
10 *
11 * 'TEXT'
12 */
13
14export const myFindExtendedFieldFunction = webMethod(Permissions.Anyone, async (displayName, dataType) => {
15
16 try {
17 const elevatedFindExtendedField = elevate(extendedFields.findOrCreateExtendedField);
18 const newExtendedField = await elevatedFindExtendedField(displayName, dataType);
19 console.log('This extended field already exists.');
20
21 return newExtendedField;
22 } catch (error) {
23 console.log(error);
24 // Handle the error
25 }
26});
27
28/*
29 * Promise resolves to:
30 * {
31 * "field": {
32 * "namespace": "custom",
33 * "key": "custom.nickname",
34 * "displayName": "Nickname",
35 * "dataType": "TEXT",
36 * "fieldType": "USER_DEFINED",
37 * "legacyId": "63408eaf-e3d0-43f3-afa5-942847d272a1",
38 * "wixSearchColumn": "info_extendedFields_custom_string_18",
39 * "_createdDate": "2023-12-25T12:21:42.000Z",
40 * "_updatedDate": "2023-12-25T12:22:25.000Z"
41 * },
42 * "newField": false
43 * }
44 */