Create computed fields that derive values from document data using formulas
Formula Fields are computed fields that automatically calculate values from other document data. Unlike AI-extracted fields, formulas run after extraction as a post-processing step, allowing you to validate, derive, enrich, and customize your document data.
| Use Case | Description |
|---|---|
| Validation | Sum line item amounts and compare to header total |
| Derivation | Calculate values that AI can't reliably extract (e.g., profit margin) |
| Enrichment | Add context from external data or complex business logic |
| Flexibility | Define custom computed fields specific to your workflow |
Sum fields automatically aggregate numeric values from line items. They are auto-generated for every numeric field in your line item schemas.
Example: Automatically sum all item amounts to calculate a total:
{
name: "calculatedTotal",
type: "sum",
displayName: "Calculated Total",
formula: {
type: "sum",
fieldPath: "items.amount",
fieldType: "currency",
currencyFieldPath: "header.currency"
}
}
Features:
Code fields allow you to write custom JavaScript to transform or enrich your data. They execute after sum fields and can access document data, file metadata, and datasets.
Example: Calculate profit margin:
{
name: "margin",
type: "code",
displayName: "Profit Margin",
formula: {
type: "code",
fieldType: "number",
inputFields: ["header.total", "header.cost"],
code: `
const revenue = header.total || 0;
const cost = header.cost || 0;
return {result: revenue > 0 ? ((revenue - cost) / revenue) * 100 : 0};
`
}
}
Features:
await fetch())Formula fields can reference datasets (XLSX or CSV files marked as datasets) to enrich your documents with external data.
Datasets are special documents (XLSX or CSV files) that are marked as Datasets in the system. They contain reference data that can be used by formula fields for lookups and enrichment.
Upload a dataset file (e.g., product-catalog.xlsx) with columns like SKU, Product Name, Price. Then use it in a formula field:
{
name: "productName",
type: "code",
displayName: "Product Name",
formula: {
type: "code",
fieldType: "text",
datasets: ["productCatalog"], // Reference your dataset
inputFields: ["sku"],
code: `
const lineItem = tx.getCurrentLineItem();
const sku = lineItem?.sku;
if (!sku) return;
// Lookup product name from dataset
const match = tx.dataset.lookup('productCatalog', 'SKU', sku);
if (match) {
tx.setResult(match['Product Name']);
}
`
}
}
"productCatalog" matches "PRODUCT_CATALOG"Formulas execute in a specific order to ensure dependencies are resolved:
This ensures that code fields can reference sum field results.
Formulas can be defined in two places:
| Location | Execution | Use Case |
|---|---|---|
| Header Schema | Once per document | Sum all line items, document-level validation |
| Line Item Schema | Once per line item | Enrich each line item with external data |
datasets arrayWhen testing a formula in the UI:
Formula fields are re-evaluated automatically after AI extraction completes. You can also manually trigger recalculation if needed.
inputFields to document which fields your formula usestx.setResult(value)tx.addMessage() for validation warnings or errors{
name: "totalValidation",
type: "code",
displayName: "Total Validation",
formula: {
type: "code",
fieldType: "boolean",
inputFields: ["header.total", "items_amount_sum"],
code: `
const headerTotal = header.total || 0;
const calculatedTotal = header.items_amount_sum || 0;
const difference = Math.abs(headerTotal - calculatedTotal);
if (difference > 0.01) {
tx.addMessage(\`Total mismatch: header shows \${headerTotal} but sum is \${calculatedTotal}\`, {
level: 'warning',
field: 'total'
});
return {result: false};
}
return {result: true};
`
}
}
{
name: "vendorCategory",
type: "code",
displayName: "Vendor Category",
formula: {
type: "code",
fieldType: "text",
datasets: ["vendorCatalog"],
inputFields: ["vendorName"],
code: `
const vendorName = header.vendorName;
if (!vendorName) return;
const match = tx.dataset.lookup('vendorCatalog', 'Vendor Name', vendorName);
if (match) {
tx.setResult(match['Category']);
}
`
}
}
On This Page
OverviewWhen to Use Formula FieldsFormula TypesSum FieldsCode FieldsUsing DatasetsWhat are Datasets?Example: SKU LookupDataset FeaturesExecution OrderFormula LocationCreating Formula FieldsIn Document Type SettingsTesting FormulasBest PracticesSum FieldsCode FieldsDatasetsExamplesExample 1: Validate Invoice TotalExample 2: Enrich with External DataNext Steps