> 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/lookup-supplier-validation.md).

# Supplier Lookup Validation

## What does this script do?

Validates the supplier number from the invoice against master data in the lookup table. If the supplier is found, their name and payment terms are automatically filled in. If not found, the field is marked as invalid so the user can correct it.

## Trigger

`AFTER_FORMATTING` on document type **INVOICE**

## Full Script

```python
# Read supplier ID from the document
supplier_id = get_field_value(document_data, "supplier_id", "")

if supplier_id:
    # Query the supplier lookup table
    records = get_lookup_records(
        org_id,                                    # Current organization
        document_json.get("sub_org_id"),           # Sub-org (if applicable)
        "supplier",                                # Lookup table name
        [["VENDOR_ID", supplier_id]],              # Filter: exact match on VENDOR_ID
        limit=1                                    # Only need the first match
    )

    if records:
        # Supplier found — auto-fill related fields
        supplier = records[0]
        set_field_value(document_data, "supplier_name", supplier.get("NAME", ""))
        set_field_value(document_data, "payment_terms", supplier.get("PAYMENT_TERMS", ""))
    else:
        # Supplier not found — mark as invalid
        set_field_as_invalid(document_data, "supplier_id",
                             f"Supplier '{supplier_id}' not found in master data")
```

## Step-by-Step Explanation

1. **Read supplier ID** from the document using `get_field_value()`
2. **Query lookup table** with `get_lookup_records()` using the vendor ID as filter
3. **On match**: Auto-fill supplier name and payment terms from master data
4. **On no match**: Mark supplier ID field as invalid with a descriptive error message

## Functions Used

* [get\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#get_field_value) — Read field value
* [get\_lookup\_records()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/business-logic-functions.md#get_lookup_records) — Query master data
* [set\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_value) — Write field value
* [set\_field\_as\_invalid()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_as_invalid) — Show validation error


---

# 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/lookup-supplier-validation.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.
