Fill Fields from a Lookup Table¶
Extraction finds a vendor code on each invoice, but the approver also needs the vendor name, the payment terms, who approves for that vendor, and the due date. The reference data is small (a few hundred vendors) and changes a few times a year. Keep it in a JSON global variable that an administrator can edit, and let a Custom Step fill the fields from it.
At a glance
- Extension point Custom Step with a JSON global variable
- You need Document fields
Index.VendorCodeandIndex.InvoiceDate(ISO date), plusIndex.VendorName,Index.PaymentTerms,Index.ApproverandIndex.DueDateon the content type; a global variableVendorTable(JSON); permission to edit the workflow and add global variables. - Teaches Parsing a JSON global variable once, a keyed lookup, date arithmetic, and a lookup status per document.
A Custom Step cannot query a database or call a service. For a large or changing table, see Use a Data Source Lookup Result.
Set it up¶
- In the Workflow app, open Global Variables. Add
VendorTable, Generic Value, data type JSON, with a value like the one below. - In the Workflow designer, add a Custom Step after extraction. Name it "Fill Vendor Fields". Paste the script below.
- Make sure
VendorName,PaymentTerms,ApproverandDueDateexist on the document's content type, so the index panel shows them. - Save the step and the workflow.
Script¶
Global variable VendorTable (JSON)
{
"V100": { "name": "Midwest Pallet & Crate Co.", "termsDays": 30, "approver": "ap-team-a" },
"V200": { "name": "Gateway Fleet Services LLC", "termsDays": 45, "approver": "ap-team-b" }
}
Custom Step: Fill Vendor Fields
// Custom Step "Fill Vendor Fields". After extraction.
// Reads: Index.VendorCode, Index.InvoiceDate (each document); global variable VendorTable (JSON).
// Writes: Index.VendorName, Index.PaymentTerms, Index.Approver, Index.DueDate,
// General.LookupStatus (each document).
(function () {
var table;
try {
table = JSON.parse(GlobalVariables.Get("VendorTable"));
} catch (e) {
throw new Error("Global variable VendorTable is missing or is not valid JSON: " + e.message);
}
function pad(n) { return n < 10 ? "0" + n : String(n); }
for (var i = 0; i < batch.Documents.length; i++) {
var doc = batch.Documents[i];
var index = doc.Metadata["Index"] || {};
var code = String(index["VendorCode"] || "").trim().toUpperCase();
var vendor = table[code];
if (!vendor) {
builder.SetDocumentMetadata(doc.DocumentId, "General", "LookupStatus", code ? "Unknown vendor " + code : "No vendor code");
continue;
}
builder.SetDocumentMetadata(doc.DocumentId, "Index", "VendorName", vendor.name);
builder.SetDocumentMetadata(doc.DocumentId, "Index", "PaymentTerms", "Net " + vendor.termsDays);
builder.SetDocumentMetadata(doc.DocumentId, "Index", "Approver", vendor.approver);
var invoiceDate = new Date(index["InvoiceDate"]);
if (!isNaN(invoiceDate.getTime())) {
// UTC methods, so the server's time zone cannot shift the date by a day.
invoiceDate.setUTCDate(invoiceDate.getUTCDate() + vendor.termsDays);
var due = invoiceDate.getUTCFullYear() + "-" + pad(invoiceDate.getUTCMonth() + 1) + "-" + pad(invoiceDate.getUTCDate());
builder.SetDocumentMetadata(doc.DocumentId, "Index", "DueDate", due);
}
builder.SetDocumentMetadata(doc.DocumentId, "General", "LookupStatus", "OK");
}
})();
How it works¶
- The script parses
VendorTableonce, before the loop. A missing variable or broken JSON fails the step with a message that says which. - The vendor code is trimmed and uppercased, so
v100findsV100. Write the table keys in uppercase. table[code]is a direct lookup, fast for hundreds of rows.- The due date adds the vendor's terms to the invoice date and writes it as
YYYY-MM-DD. An ISO date such as2026-10-01is read as midnight UTC, so the script does the arithmetic in UTC. General.LookupStatusrecordsOK,Unknown vendor ...orNo vendor codeon every document. Route on it, or show it to the indexer.
Test it¶
- Upload one invoice with vendor code
V100and invoice date2026-10-01, and one with codeV999. - Open the work item in Monitor or Insight and check both documents.
You should see
on the first document Index.VendorName = Midwest Pallet & Crate Co., Index.PaymentTerms = Net 30, Index.DueDate = 2026-10-31 and General.LookupStatus = OK; on the second, General.LookupStatus = Unknown vendor V999.
What can go wrong¶
Watch out
- The step fails with "VendorTable is missing or is not valid JSON". The variable does not exist, or its value has a syntax error such as a trailing comma.
- A table change has no effect. Work items read the global variable version from when they were submitted. Test with a new work item.
- The fields stay empty in the index panel.
VendorName,PaymentTerms,ApproverandDueDatemust exist on the document's content type. - The table keeps growing. A JSON global variable is for small reference data. When it reaches thousands of rows or changes daily, move it to a data source. See Use a Data Source Lookup Result.