# Conditional custom fields This guide explains how to set a custom field to only appear only when the traveler gives a particular answer to another custom field. For example, a company might ask whether a traveler is extending their stay, and collect an emergency contact number only if the traveler answers **Yes**. Be sure to read [custom field concepts](/spotnana/basic_custom_field_concepts) to familiarize yourself with this feature before starting to use it. ## Creating the main custom field Create the custom field that asks a question. For example, **Are you planning to extend your stay?**. You would do this using the [create custom field](/openapi/customfieldapi/custom-field-v3/createcustomfieldv3) endpoint as shown below: ```json POST /v3/companies/{companyId}/custom-fields?companyRole=ORG { "name": "Are you planning to extend your stay?", "description": { "value": "Captures whether the traveler stays beyond the event dates" } } ``` Add an arm with a [create custom field arm](/openapi/customfieldapi/custom-field-v3/createarm) request. This arm has no audience conditions, so it applies to every traveler who makes a booking. The `action` is set to `USER` and the option source `type` is set to `MANUAL` as it requires a manual response from the traveler. The API response contains an `optionGroupId` which will be used in [creating a dependent custom field](#creating-the-dependent-custom-field). ```json POST /v3/companies/{companyId}/custom-fields/{customFieldId}/arms { "name": "All travelers", "action": { "type": "USER", "required": true, "format": { "type": "OPTION_LIST", "optionListTypeFormat": { "isMultiSelect": false, "isPctType": false }, "customValueFormat": { "isCustomValueAllowed": false } }, "userOptionSource": { "type": "MANUAL" } } } ``` Add the two answers with an [update options](/openapi/customfieldapi/custom-field-v3/updatecustomfieldoptiongroupv3) API call. The `name` of each option will be displayed to the traveler for them to choose. ```json PATCH /v3/companies/{companyId}/custom-fields/{customFieldId}/option-groups/{optionGroupId} { "toUpdate": [ { "name": "Yes" }, { "name": "No" } ] } ``` ## Creating the dependent custom field Create the second custom field using the same [create custom field](/openapi/customfieldapi/custom-field-v3/createcustomfieldv3) endpoint. ```json POST /v3/companies/{companyId}/custom-fields?companyRole=ORG { "name": "Enter your emergency contact number", "description": { "value": "Collected only when the traveler extends their stay" } } ``` [Create a custom field arm](/openapi/customfieldapi/custom-field-v3/createarm) to include it's dependency on the [main custom field](#creating-the-main-custom-field). Set the following values: * `audience` > `predicates` > `type` as `CUSTOM_FIELD_RESPONSE` * `id` as the `optionGroupId` of the main custom field's [arm](#creating-the-main-custom-field) * `comparator` as `EQ` * `values` as `Yes`. The above set of conditions translates to: *Answers where the main custom field response equals to **Yes***. Now that we have the condition, set the action in the same API request as follows: * `action` > `type` as `USER` (i.e., requires a user input) * `format` > `type` as `TEXT_INPUT` (i.e., the user needs to type into a text box) * `allowedChars` as `NUMERIC` (i.e., only allow numbers since the user is entering their phone number). Below is a sample request where all the fields mentioned above are being set as described: ```json POST /v3/companies/{companyId}/custom-fields/{customFieldId}/arms { "name": "Travelers extending their stay", "audiences": [ { "name": "Answered Yes to the extended stay question", "predicates": [ { "type": "CUSTOM_FIELD_RESPONSE", "id": "c73b1e58-9d24-4f07-a6b8-1e5c9d380f2a", "comparator": "EQ", "values": [ "Yes" ] } ] } ], "action": { "type": "USER", "required": true, "format": { "type": "TEXT_INPUT", "userInputFormat": { "type": "NON_REGEX", "allowedChars": "NUMERIC", "minLength": 7, "maxLength": 15 } } } } ``` Every new custom field is disabled by default. Use the [update custom field status](/openapi/customfieldapi/custom-field-v3/updatecustomfieldstatus) endpoint to enable the custom fields using their `ids`.