> 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/table-sum-validation.md).

# Table Sum Validation

## What does this script do?

Validates that the sum of all line totals in the invoice table matches the document's net amount. If there is a discrepancy greater than 0.01, the calculated sum replaces the extracted net amount — ensuring consistency between line items and header fields.

## Trigger

`AFTER_FORMATTING` on document type **INVOICE**

## Full Script

```python
table = tables_dict.get("INVOICE_TABLE")
if table:
    # Calculate sum of all line totals
    total = 0
    for row in table["rows"]:
        line_total = get_column_value(row, "LINE_TOTAL", "0")
        try:
            total += float(line_total)
        except ValueError:
            pass

    # Compare with extracted net amount
    net_amount = get_field_value(document_data, "net_amount", "0")
    try:
        if abs(float(net_amount) - total) > 0.01:
            # Line sum differs from header — update net amount
            set_amount_value(document_data, "net_amount", str(round(total, 2)))
    except ValueError:
        pass
```

## Variation: Mark as invalid instead of overwriting

```python
table = tables_dict.get("INVOICE_TABLE")
if table:
    total = 0
    for row in table["rows"]:
        line_total = get_column_value(row, "LINE_TOTAL", "0")
        try:
            total += float(line_total)
        except ValueError:
            pass

    net_amount = get_field_value(document_data, "net_amount", "0")
    try:
        diff = abs(float(net_amount) - total)
        if diff > 0.01:
            set_field_as_invalid(document_data, "net_amount",
                f"Line total sum ({round(total, 2)}) differs from net amount ({net_amount})")
        else:
            set_field_as_valid(document_data, "net_amount", "Amounts match")
    except ValueError:
        pass
```

## Step-by-Step Explanation

1. **Get invoice table** from `tables_dict`
2. **Sum all LINE\_TOTAL values** across table rows
3. **Compare** calculated sum with the extracted net amount
4. **Update or flag** — either replace the net amount or mark it as invalid

## Functions Used

* [get\_column\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/table-functions.md#get_column_value) — Read column values from rows
* [get\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#get_field_value) — Read net amount
* [set\_amount\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_amount_value) — Set corrected amount
* [set\_field\_as\_invalid()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_as_invalid) — Mark field as invalid
* [set\_field\_as\_valid()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_as_valid) — Mark field as valid


---

# 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/table-sum-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.
