> For the complete documentation index, see [llms.txt](https://docs.docbits.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.docbits.com/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/sample-scripts/dynamic-required-fields.md).

# Dynamic Required Fields

## What does this script do?

Dynamically sets field requirements based on document content. In this example: when the invoice currency is not EUR, the exchange rate field becomes mandatory and visible. For EUR invoices, the exchange rate field is hidden and optional.

## Trigger

`ON_FIELD_CHANGE` on document type **INVOICE**

## Full Script

```python
# Read current currency
currency = get_field_value(document_data, "currency", "EUR")

# Foreign currency: exchange rate is required and visible
if currency and currency != "EUR":
    set_is_required(document_data, "exchange_rate", True)
    set_is_hidden(document_data, "exchange_rate", False)
else:
    # EUR: exchange rate is optional and hidden
    set_is_required(document_data, "exchange_rate", False)
    set_is_hidden(document_data, "exchange_rate", True)
```

## Variation: Purchase invoice vs. cost invoice

```python
po = get_field_value(document_data, "purchase_order", "")

if po and po.strip():
    # Purchase invoice: PO number is required
    set_field_value(document_data, "invoice_category", "PURCHASE_INVOICE")
    set_is_required(document_data, "purchase_order", True)
else:
    # Cost invoice: PO number not needed, hide table
    set_field_value(document_data, "invoice_category", "COST_INVOICE")
    set_is_required(document_data, "purchase_order", False)
    delete_tables(document_data)
```

## Step-by-Step Explanation

1. **Read the controlling field** (currency in this case)
2. **Apply business rules** — different field requirements based on the value
3. **Set visibility** — hide irrelevant fields to keep the UI clean
4. **Set requirements** — make relevant fields mandatory

{% hint style="info" %}
**Trigger choice:** `ON_FIELD_CHANGE` runs every time a user modifies a field, so the requirements update in real-time. `AFTER_FORMATTING` only runs once after initial extraction.
{% endhint %}

## Functions Used

* [get\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#get_field_value) — Read controlling field
* [set\_is\_required()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_is_required) — Set field as mandatory/optional
* [set\_is\_hidden()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_is_hidden) — Show/hide fields
* [set\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_value) — Set category field
* [delete\_tables()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/table-functions.md#delete_tables) — Remove tables for cost invoices


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.docbits.com/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/sample-scripts/dynamic-required-fields.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
