> 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/duplicate-invoice-detection.md).

# Duplicate Invoice Detection

{% hint style="info" %}
**Available from version 11.48.0** — Requires `OPENSEARCH_ENABLED` license.
{% endhint %}

## What does this script do?

Searches the document archive for existing invoices with the same invoice number from the same vendor. If a potential duplicate is found, the invoice number field is marked as invalid with a warning showing the duplicate document name and status.

## Trigger

`AFTER_FORMATTING` on document type **INVOICE**

## Full Script

```python
inv_id = get_field_value(document_data, "invoice_id", "")
vendor = get_field_value(document_data, "supplier_name", "")

if inv_id and vendor:
    # Search for documents with the same invoice number from the same vendor
    existing = fulltext_search(
        inv_id,
        vendor_name=vendor,
        status="ready_for_validation,exported",
        size=5
    )

    # Exclude the current document from results
    current_doc_id = document_json["doc_id"]
    duplicates = [d for d in existing if d["doc_id"] != current_doc_id]

    if duplicates:
        dup = duplicates[0]
        set_field_as_invalid(
            document_data, "invoice_id",
            f"Possible duplicate: {dup['name']} ({dup.get('status', 'unknown')})"
        )
```

## Step-by-Step Explanation

1. **Read invoice number and vendor** from the current document
2. **Search the archive** with `fulltext_search()` filtering by vendor name and relevant statuses
3. **Exclude current document** from results to avoid self-matching
4. **Mark as invalid** if any duplicate is found, showing the filename and status of the existing document

## 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
* [fulltext\_search()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/fulltext-search-functions.md#fulltext_search) — Search OCR text across all documents
* [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/duplicate-invoice-detection.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.
