> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> 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`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# FindOrCreateExtendedField

# Package: contacts

# Namespace: ContactExtendedFieldsServiceV4

# Method link: https://dev.wix.com/docs/api-reference/crm/members-contacts/contacts/extended-fields/find-or-create-extended-field.md

## Permission Scopes:
Manage Contact Extended Fields: SCOPE.DC-CONTACTS.MANAGE-EX-FIELDS

## Introduction

Retrieves a custom field with a specified name, or creates one if it doesn't exist.
The number of custom fields is limited to 100.

Successful calls to this method always return a field,
which can be specified in subsequent calls.

To find an existing custom field without potentially creating a new one, call Get Extended Field or
Query Extended Fields.

---

## REST API

### Schema

```
 Method: findOrCreateExtendedField
 Description: Retrieves a custom field with a specified name, or creates one if it doesn't exist. The number of custom fields is limited to 100.  Successful calls to this method always return a field, which can be specified in subsequent calls.  To find an existing custom field without potentially creating a new one, call Get Extended Field or Query Extended Fields.
 URL: https://www.wixapis.com/contacts/v4/extended-fields
 Method: POST
 # Note: If the parameter `a.b` is listed under required parameters, `b` is only required if `a` is also present.
 Required parameters:  displayName
 Method parameters: 
   param name: dataType | type: FieldDataType    
      - enum:
           TEXT - Accepts strings.
           NUMBER - Accepts floats.
           DATE - Accepts dates formatted as `YYYY-MM-DD`.
           URL - Accepts strings. Prepends `https://` if no protocol is included.
   param name: displayName | type: displayName | description: Display name to retrieve or create.  If an existing custom field is an exact match for the specified display name, the existing field is returned. If not, a new field is created and returned. | required: true | validation: minLength 1, maxLength 150
 Return type: FindOrCreateExtendedFieldResponse
  - name: field | type: ExtendedField | description: Extended field that was found or created.  
     - name: namespace | type: string | description: Extended field namespace.  Extended fields created through by calling the Find Or Create Extended Field method are automatically assigned to the `custom` namespace.  | read-only: true | validation: minLength 1, maxLength 25
     - name: key | type: string | description: Extended field key.  When accessing contact data, extended field data is available at `extendedFields[key]`. For example, if the key is "custom.notes", the value can be accessed at `extendedFields["custom.notes"]`.  `key` is generated when the extended field is created and can't be modified, even if `displayName` changes.  | read-only: true | validation: minLength 1, maxLength 80
     - name: displayName | type: string | description: Display name shown in the contact list.  | validation: minLength 1, maxLength 150
     - name: dataType | type: FieldDataType | description: Type of data the field holds.  | read-only: true 
         - enum:
         -     TEXT: Accepts strings.
         -     NUMBER: Accepts floats.
         -     DATE: Accepts dates formatted as `YYYY-MM-DD`.
         -     URL: Accepts strings. Prepends `https://` if no protocol is included.
     - name: fieldType | type: FieldType | description: Indicates whether the extended field is a system field or custom field.  | read-only: true 
         - enum:
         -     SYSTEM: Field is a system field managed by Wix. System fields can't be modified by calling the Update Extended Field method.
         -     USER_DEFINED: Field is a custom field and can be modified by calling the Update Extended Field method.
     - name: createdDate | type: string | description: Date and time the field was created.  | read-only: true | validation: format date-time
     - name: updatedDate | type: string | description: Date and time the field was last updated.  | read-only: true | validation: format date-time
     - name: description | type: string | description: Field description, if the field is a system field.  | read-only: true | validation: minLength 1, maxLength 150
  - name: newField | type: boolean | description: Indicates whether the extended field was just created or already existed.  Returns `true` if the field was just created.  

 Possible Errors:
   HTTP Code: 429 | Status Code: RESOURCE_EXHAUSTED | Application Code: FIELDS_QUOTA_EXCEEDED | Description: The site reached the limit of 100 custom fields. Some fields must be deleted before any more can be created.


```

### Examples

### Find or Create Extended Field
```curl
curl -X POST 'https://www.wixapis.com/contacts/v4/extended-fields' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: <AUTH>' \
  --data-binary '{
    "displayName": "My Field"
    "dataType" : "TEXT"
  }'
```

---

## JavaScript SDK

### Schema

```
 Method: wixClientAdmin.crm.extendedFields.findOrCreateExtendedField(displayName, dataType)
 Description: Retrieves a custom field with a specified name, or creates one if it doesn't exist. The number of custom fields is limited to 100.  Successful calls to this method always return a field, which can be specified in subsequent calls.  To find an existing custom field without potentially creating a new one, call Get Extended Field or Query Extended Fields.
 # Note: If the parameter `a.b` is listed under required parameters, `b` is only required if `a` is also present.
 Required parameters:  displayName, dataType
 Method parameters: 
   param name: displayName | type: string | description: Display name to retrieve or create.  If an existing custom field is an exact match for the specified display name, the existing field is returned. If not, a new field is created and returned. | required: true | validation: minLength 1, maxLength 150
   param name: dataType | type: FieldDataType   | required: true 
      - enum:
           TEXT - Accepts strings.
           NUMBER - Accepts floats.
           DATE - Accepts dates formatted as `YYYY-MM-DD`.
           URL - Accepts strings. Prepends `https://` if no protocol is included.
 Return type: PROMISE<FindOrCreateExtendedFieldResponse>
  - name: field | type: ExtendedField | description: Extended field that was found or created.  
     - name: namespace | type: string | description: Extended field namespace.  Extended fields created through by calling the Find Or Create Extended Field method are automatically assigned to the `custom` namespace.  | read-only: true | validation: minLength 1, maxLength 25
     - name: key | type: string | description: Extended field key.  When accessing contact data, extended field data is available at `extendedFields[key]`. For example, if the key is "custom.notes", the value can be accessed at `extendedFields["custom.notes"]`.  `key` is generated when the extended field is created and can't be modified, even if `displayName` changes.  | read-only: true | validation: minLength 1, maxLength 80
     - name: displayName | type: string | description: Display name shown in the contact list.  | validation: minLength 1, maxLength 150
     - name: dataType | type: FieldDataType | description: Type of data the field holds.  | read-only: true 
         - enum:
         -     TEXT: Accepts strings.
         -     NUMBER: Accepts floats.
         -     DATE: Accepts dates formatted as `YYYY-MM-DD`.
         -     URL: Accepts strings. Prepends `https://` if no protocol is included.
     - name: fieldType | type: FieldType | description: Indicates whether the extended field is a system field or custom field.  | read-only: true 
         - enum:
         -     SYSTEM: Field is a system field managed by Wix. System fields can't be modified by calling the Update Extended Field method.
         -     USER_DEFINED: Field is a custom field and can be modified by calling the Update Extended Field method.
     - name: _createdDate | type: Date | description: Date and time the field was created.  | read-only: true 
     - name: _updatedDate | type: Date | description: Date and time the field was last updated.  | read-only: true 
     - name: description | type: string | description: Field description, if the field is a system field.  | read-only: true | validation: minLength 1, maxLength 150
  - name: newField | type: boolean | description: Indicates whether the extended field was just created or already existed.  Returns `true` if the field was just created.  

 Possible Errors:
   HTTP Code: 429 | Status Code: RESOURCE_EXHAUSTED | Application Code: FIELDS_QUOTA_EXCEEDED | Description: The site reached the limit of 100 custom fields. Some fields must be deleted before any more can be created.


```

### Examples

### Find an extended field (with elevated permissions)
```javascript
import { extendedFields } from '@wix/crm';
import { auth } from '@wix/essentials';

/* Sample displayName value: 
 *  
 *  'Nickname'
 * 
 * Sample dataType value: 
 * 
 *  'TEXT'
 */

export async function myFindExtendedFieldFunction(displayName, dataType) {
  
  try {
    const elevatedFindExtendedField = auth.elevate(extendedFields.findOrCreateExtendedField);
    const newExtendedField = await elevatedFindExtendedField(displayName, dataType);    
    console.log('This extended field already exists.');
 
    return newExtendedField;  
  } catch (error) {
    console.log(error);
    // Handle the error
  }
}

/*
 * Promise resolves to:
 * {
 *   "field": {
 *     "namespace": "custom",
 *     "key": "custom.nickname",
 *     "displayName": "Nickname",
 *     "dataType": "TEXT",
 *     "fieldType": "USER_DEFINED",
 *     "legacyId": "63408eaf-e3d0-43f3-afa5-942847d272a1",
 *     "wixSearchColumn": "info_extendedFields_custom_string_18",
 *     "_createdDate": "2023-12-25T12:21:42.000Z",
 *     "_updatedDate": "2023-12-25T12:22:25.000Z"
 *   },
 *   "newField": false
 * }
 */
```

### Find an extended field
```javascript
import { extendedFields } from '@wix/crm';

/* Sample displayName value: 
 *  
 *  'Nickname'
 * 
 * Sample dataType value: 
 * 
 *  'TEXT'
 */

export async function myFindExtendedFieldFunction(displayName, dataType) {
  
  try {
    const newExtendedField = await extendedFields.findOrCreateExtendedField(displayName, dataType);    
    console.log('This extended field already exists.');
 
    return newExtendedField;  
  } catch (error) {
    console.log(error);
    // Handle the error
  }
}

/*
 * Promise resolves to:
 * {
 *   "field": {
 *     "namespace": "custom",
 *     "key": "custom.nickname",
 *     "displayName": "Nickname",
 *     "dataType": "TEXT",
 *     "fieldType": "USER_DEFINED",
 *     "legacyId": "63408eaf-e3d0-43f3-afa5-942847d272a1",
 *     "wixSearchColumn": "info_extendedFields_custom_string_18",
 *     "_createdDate": "2023-12-25T12:21:42.000Z",
 *     "_updatedDate": "2023-12-25T12:22:25.000Z"
 *   },
 *   "newField": false
 * }
 */
```

### Create an extended field (with elevated permissions)
```javascript
import { extendedFields } from '@wix/crm';
import { auth } from '@wix/essentials';

/* Sample displayName value: 
 *  
 *  'Age'
 * 
 * Sample dataType value: 
 * 
 *  'NUMBER'
 */

const elevatedFindOrCreateExtendedField = auth.elevate(extendedFields.findOrCreateExtendedField);

export async function myCreateExtendedFieldFunction(displayName, dataType) {
  
  try {
    const newExtendedField = await elevatedFindOrCreateExtendedField(displayName, dataType);    
    console.log('Successfully created new extended field:', newExtendedField)
 
    return newExtendedField;  
  } catch (error) {
    console.log(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "field": {
 *     "namespace": "custom",
 *     "key": "custom.age",
 *     "displayName": "Age",
 *     "dataType": "NUMBER",
 *     "fieldType": "USER_DEFINED",
 *     "legacyId": "ed349d8c-b2bc-46a4-80d8-7632c6f50b00",
 *     "wixSearchColumn": "info_extendedFields_custom_double_27",
 *     "_createdDate": "2023-12-25T12:16:40.000Z",
 *     "_updatedDate": "2023-12-25T12:16:40.000Z"
 *   },
 *   "newField": false
 * }
 */
```

### Create an extended field
```javascript
import { extendedFields } from '@wix/crm';

/* Sample displayName value: 
 *  
 *  'Age'
 * 
 * Sample dataType value: 
 * 
 *  'NUMBER'
 */

export async function myCreateExtendedFieldFunction(displayName, dataType) {
  
  try {
    const newExtendedField = await extendedFields.findOrCreateExtendedField(displayName, dataType);    
    console.log('Successfully created new extended field:', newExtendedField)
 
    return newExtendedField;  
  } catch (error) {
    console.log(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "field": {
 *     "namespace": "custom",
 *     "key": "custom.age",
 *     "displayName": "Age",
 *     "dataType": "NUMBER",
 *     "fieldType": "USER_DEFINED",
 *     "legacyId": "ed349d8c-b2bc-46a4-80d8-7632c6f50b00",
 *     "wixSearchColumn": "info_extendedFields_custom_double_27",
 *     "_createdDate": "2023-12-25T12:16:40.000Z",
 *     "_updatedDate": "2023-12-25T12:16:40.000Z"
 *   },
 *   "newField": false
 * }
 */
```

### findOrCreateExtendedField (self-managed)
Self-managed SDK calls require you to [create a client](https://dev.wix.com/docs/sdk/articles/work-with-the-sdk/about-the-wix-client.md).

```javascript
import { createClient } from '@wix/sdk';
import { extendedFields } from '@wix/crm';
// Import the auth strategy for the relevant access type
// Import the relevant host module if needed

const myWixClient = createClient ({
  modules: { extendedFields },
  // Include the auth strategy and host as relevant
});


async function findOrCreateExtendedField(displayName,dataType) {
  const response = await myWixClient.extendedFields.findOrCreateExtendedField(displayName,dataType);
};
```

---