findOrCreateExtendedField( )
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()
.
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
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.
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 asYYYY-MM-DD
."URL"
: Accepts strings. Prependshttps://
if no protocol is included.
Returns
Extended field that was found or created.
Return Type:
NAME
TYPE
DESCRIPTION
Extended field that was found or created.
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?
1import { extendedFields } from 'wix-crm.v2';23/* Sample displayName value:4 *5 * 'Age'6 *7 * Sample dataType value:8 *9 * 'NUMBER'10 */1112export async function myCreateExtendedFieldFunction(displayName, dataType) {1314 try {15 const newExtendedField = await extendedFields.findOrCreateExtendedField(displayName, dataType);16 console.log('Successfully created new extended field:', newExtendedField)1718 return newExtendedField;19 } catch (error) {20 console.log(error);21 // Handle the error22 }23}2425/* 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": false39 * }40 */
1import { Permissions, webMethod } from 'wix-web-module';2import { extendedFields } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample displayName value:6 *7 * 'Age'8 *9 * Sample dataType value:10 *11 * 'NUMBER'12 */1314export const myCreateExtendedFieldFunction = webMethod(Permissions.Anyone, async (displayName, dataType) => {1516 try {17 const elevatedCreateExtendedField = elevate(extendedFields.findOrCreateExtendedField);18 const newExtendedField = await elevatedCreateExtendedField(displayName, dataType);19 console.log('Successfully created new extended field:', newExtendedField)2021 return newExtendedField;22 } catch (error) {23 console.log(error);24 // Handle the error25 }26});2728/* 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": false42 * }43 */44
1import { Permissions, webMethod } from 'wix-web-module';2import { extendedFields } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample displayName value:6 *7 * 'Nickname'8 *9 * Sample dataType value:10 *11 * 'TEXT'12 */1314export const myFindExtendedFieldFunction = webMethod(Permissions.Anyone, async (displayName, dataType) => {1516 try {17 const elevatedFindExtendedField = elevate(extendedFields.findOrCreateExtendedField);18 const newExtendedField = await elevatedFindExtendedField(displayName, dataType);19 console.log('This extended field already exists.');2021 return newExtendedField;22 } catch (error) {23 console.log(error);24 // Handle the error25 }26});2728/*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": false43 * }44 */