githubEdit

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

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

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

Last updated

Was this helpful?