# Custom fields

Source: https://swapcard.dev/content-api/examples/custom-fields


List, create, update and delete the custom fields of an event, and manage the values of select fields. Custom fields add information to people, exhibitors, sessions and products, so you can show it on pages, build filters and improve matchmaking results.

Reference: [`CreateFieldDefinitionV2Input`](../graphql-event-api-schema/inputs/create-field-definition-v2-input.mdx)

On this page you can:

- [List the custom fields](#get-custom-fields-of-an-event) of an event.
- [List the values of select fields](#get-all-values-of-a-single-or-multiple-choices-custom-field-on-the-event).
- [Create](#create-custom-fields), [update](#update-custom-fields) and [delete](#delete-custom-field) custom fields.
- [Add a value to a select field](#create-custom-fields-value-for-multiple-or-single-select-type-custom-fields) and [delete select field values](#delete-a-single-or-multiple-choice-values-of-a-custom-field).

## Custom field types

A custom field applies to one entity, its **target**: People, Exhibitors, Sessions (`plannings` in the API) or Products (`items`). Each field has one of the following types:

| Type | `type` value |
| --- | --- |
| Number | `NUMBER` |
| URL | `URL` |
| Media (picture) | `MEDIA` |
| Text | `TEXT` |
| Long text | `LONG_TEXT` |
| Select (single choice) | `SELECT` |
| Multiple select (multiple choices) | `MULTIPLE_SELECT` |
| Multiple text | `MULTIPLE_TEXT` |
| Tree | `TREE` |
| Date | `DATE` |

:::note[Tree fields]
Tree fields have their own page. See [Tree fields](./graphql-event-api-examples-tree-fields.md) to create and manage the tree structure with `createFieldDefinition`, `updateFieldDefinition`, `createTreeFieldNode`, `updateTreeFieldNode`, `moveTreeFieldNode` and `deleteTreeFieldNode`.
:::

## Before you begin

- An API key, sent in the `Authorization` header. The key acts with the roles and permissions of the user who created it. See [Authentication](/organizer/authentication).
- The ID of your event (`eventId`). See [Events](./graphql-event-api-examples-event.md).

## List custom fields
Use this query to fetch all the custom fields set up on an event, with their ID and name.

| Argument | Type | Description |
| --- | --- | --- |
| `eventId` | `ID!` | The ID of the event. |
| `target` | [`FieldDefinitionTargetEnum`](../graphql-event-api-schema/enums/field-definition-target-enum.mdx) | Optional. Returns only the custom fields of one entity: `PEOPLE`, `EXHIBITORS`, `PLANNINGS` or `PRODUCTS`. |

```graphql title="Query"
query AllEventCustomFields($eventId: ID!, $target: FieldDefinitionTargetEnum!) {
  event(id: $eventId) {
    fieldDefinitions(target: $target) {
      ... on NumberFieldDefinition {
        id
        name
      }
      ... on UrlFieldDefinition {
        id
        name
      }
      ... on MediaFieldDefinition {
        id
        name
      }
      ... on TextFieldDefinition {
        id
        name
      }
      ... on LongTextFieldDefinition {
        id
        name
      }
      ... on SelectFieldDefinition {
        id
        name
      }
      ... on MultipleSelectFieldDefinition {
        id
        name
      }
      ... on MultipleTextFieldDefinition {
        id
        name
      }
      ... on TreeFieldDefinition {
        id
        name
        tree {
          value
          path {
            value
            translations {
              language
              value
            }
          }
        }
      }
      ... on DateFieldDefinition {
        id
        name
      }
    }
  }
}
```

```json title="Variables"
{
  "eventId": "RXZlbnRfMQ==",
  "target": "EXHIBITORS"
}
```

## List the values of select fields
Use this query to fetch every option value of the single choice (`SELECT`) and multiple choice (`MULTIPLE_SELECT`) custom fields of an event.

| Argument | Type | Description |
| --- | --- | --- |
| `eventId` | `ID!` | The ID of the event. |

```graphql title="Query"
query EventCustomFields($eventId: ID!) {
  event(id: $eventId) {
    fieldDefinitions {
      ... on SelectFieldDefinition {
        id
        name
        optionsValues {
          value
          id
        }
      }
      ... on MultipleSelectFieldDefinition {
        id
        name
        optionsValues {
          id
          value
        }
      }
    }
  }
}
```

```json title="Variables"
{
  "eventId": "RXZlbnRfMQ=="
}
```

## Create a custom field
Use the `createFieldDefinition` mutation to create a custom field on people, exhibitors, sessions or products.

| Argument | Type | Description |
| --- | --- | --- |
| `input.eventId` | `ID` | The ID of the event on which to create the custom field. |
| `input.name` | `String!` | The name of the custom field. |
| `input.target` | [`FieldDefinitionTargetEnum!`](../graphql-event-api-schema/enums/field-definition-target-enum.mdx) | The entity the custom field applies to: `PEOPLE`, `EXHIBITORS`, `PLANNINGS` or `PRODUCTS`. |
| `input.type` | `FieldTypeEnum!` | The data type of the custom field. See [Custom field types](#custom-field-types). |

Except for People, select and multiple select fields must first be created, in Studio or with the Content API, and their values set with the [`setSelectFieldValue` mutation](#create-custom-fields-value-for-multiple-or-single-select-type-custom-fields).

```graphql title="Mutation"
mutation createFieldDefinition($input: CreateFieldDefinitionV2Input!) {
  createFieldDefinition(input: $input) {
    event {
      id
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "eventId": "RXZlbnRfMQ==",
    "name": "Where did you hear about this event?",
    "translations": {
      "language": "en_US",
      "name": "Where did you hear about this event?",
      "placeholder": "Select how do you discover the event"
    },
    "target": "PEOPLE",
    "type": "SELECT",
    "isEditable": true,
    "isVisible": true
  }
}
```

## Update a custom field
Use the `updateFieldDefinition` mutation to change the settings of an existing custom field.

| Argument | Type | Description |
| --- | --- | --- |
| `input.fieldDefinitionId` | `ID!` | The ID of the custom field to update. |

```graphql title="Mutation"
mutation updateFieldDefinition($input: UpdateFieldDefinitionV2Input!) {
  updateFieldDefinition(input: $input) {
    event {
      id
    }
    errors {
      code
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "fieldDefinitionId": "RmllbGREZWZl0aW9uXzQwNDAxNg==",
    "maxCharacters": 200,
    "isEditable": true,
    "isVisible": true
  }
}
```

## Add a value to a select field
Use the `setSelectFieldValue` mutation to add an option value to a single or multiple select custom field.

| Argument | Type | Description |
| --- | --- | --- |
| `fieldDefinitionId` | `ID!` | The ID of the custom field. The field must already exist. |
| `key` | `String!` | The key of the value: the identifier attached to it. In Studio, you can see it by hovering over the value in the custom field settings. |
| `translations` | `[SelectFieldValueTranslationInput!]` | The value in each language. |

You use keys to apply a value to a specific person, exhibitor, session or product.

When you create a value in Studio, its key is generated from the name: lowercased, with diacritics removed, then slugified. For example, the value `São Tomé and Príncipe` gets the key `sao-tome-and-principe`.

:::tip
If you create values both in Studio and through the API, apply the same logic to the keys you create through the API to avoid duplicates.
:::

```graphql title="Mutation"
mutation setSelectFieldValue($data: SetSelectFieldValueInput!) {
  setSelectFieldValue(input: $data) {
    selectField {
      ... on MultipleSelectField {
        id
        value
        translations {
          language
          value
        }
      }
    }
  }
}
```

```json title="Variables"
{
  "data": {
    "fieldDefinitionId": "RmllbGREZWZpbml0aW9uXEzNg==",
    "key": "india",
    "translations": [
      {
        "language": "en_US",
        "value": "INDIA"
      },
      {
        "language": "fr_FR",
        "value": "INDE"
      },
      {
        "language": "es_ES",
        "value": "INDIAS"
      }
    ]
  }
}
```

## Delete custom fields
Use the `deleteFieldDefinitions` mutation to delete one or more custom fields.

| Argument | Type | Description |
| --- | --- | --- |
| `input.eventId` | `ID` | The ID of the event to delete the custom fields from. |
| `input.fieldDefinitionIds` | `[ID!]!` | The IDs of the custom fields to delete. |

```graphql title="Mutation"
mutation deleteFieldDefinition($input: DeleteFieldDefinitionsInput!) {
  deleteFieldDefinitions(input: $input) {
    errors {
      code
    }
    event {
      id
    }
  }
}
```

```json title="Variables"
{
  "input": {
    "eventId": "RXZlbnRfMQ==",
    "fieldDefinitionIds": [
      "RmllbGREZWZpbml0aW9uXzQwNDAxNg==",
      "RmllbGREZWZpbml0aS3uXzQwNDAxNg=="
    ]
  }
}
```

## Delete select field values
Use the `deleteSelectFieldValues` mutation to delete option values from a single or multiple select custom field.

| Argument | Type | Description |
| --- | --- | --- |
| `input.fieldDefinitionId` | `ID!` | The ID of the custom field. |
| `input.keys` | `[String!]!` | The keys of the values to delete. |

```graphql title="Mutation"
mutation deleteSelectFieldValues($input: DeleteSelectFieldValuesInput!) {
  deleteSelectFieldValues(input: $input) {
    deletedSelectFieldsIds
  }
}
```

```json title="Variables"
{
  "input": {
    "fieldDefinitionId": "RmllbGREZWZpbml0aW9MzMg==",
    "keys": "testeposters"
  }
}
```
