> 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/auto-po-matching.md).

# Auto PO Matching

## What does this script do?

Automatically triggers PO (Purchase Order) matching when a PO number is present on the invoice. The po-match-service microservice compares invoice line items against the PO and populates match results.

## Trigger

`AFTER_FORMATTING` on document type **INVOICE**

## Full Script

```python
# Read PO number from the document
po_nr = get_field_value(document_data, "purchase_order", "")

if po_nr:
    # Clean up PO number: remove prefix and whitespace
    po_nr = po_nr.strip()
    if po_nr.upper().startswith("PO"):
        po_nr = po_nr[2:].strip()
    if po_nr.startswith("-") or po_nr.startswith(" "):
        po_nr = po_nr[1:].strip()

    # Update cleaned PO number
    set_field_value(document_data, "purchase_order", po_nr)

    # Trigger automatic PO matching
    auto_po_match_for_purchase_orders(user, document_data, po_nr)
```

## Step-by-Step Explanation

1. **Read PO number** from the invoice
2. **Clean up** the PO number by removing common prefixes like "PO-" or "PO "
3. **Update** the cleaned PO number back to the document
4. **Trigger PO matching** which calls the po-match-service to compare invoice lines against PO lines

## What happens after matching?

The `document_data` is updated with:

* `po_items` — Matched PO line items
* `po_match_status` — Match result (`"matched"`, `"partially_matched"`, etc.)
* `po_multi_matched` — Whether multiple POs were matched

## 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
* [set\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_value) — Write cleaned PO number
* [auto\_po\_match\_for\_purchase\_orders()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/business-logic-functions.md#auto_po_match_for_purchase_orders) — Trigger PO matching


---

# 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/auto-po-matching.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.
