Set a Routing Value from Metadata¶
An accounts payable team routes invoices by value:
- Any invoice over the manager limit needs a manager.
- A work item whose total is over the director limit needs a director.
- A new vendor always needs a manager.
Everything else is approved straight through. The limits change every budget year, so they live in global variables. The Custom Step makes the whole decision once and writes one routing value; the routes only compare it.
At a glance
- Extension point Custom Step with routes
- You need Document field
Index.InvoiceTotal; work-item fieldIndex.VendorStatus(for exampleNeworApproved); global variablesManagerLimitandDirectorLimit(Decimal); a manager task, a director task and an approved path; permission to edit the workflow and add global variables. - Teaches per-document flags plus a work-item decision, limits from global variables, and simple routes on one value.
Set it up¶
- In the Workflow app, open Global Variables. Add
ManagerLimit(Generic Value, Decimal,10000) andDirectorLimit(Decimal,50000). - In the Workflow designer, add a Custom Step after extraction. Name it "Approval Level". Paste the script below.
- From "Approval Level", draw three routes:
- To the director task, a Javascript route:
(workItem.Metadata["General"] || {})["ApprovalLevel"] === "Director" - To the manager task, a Javascript route:
(workItem.Metadata["General"] || {})["ApprovalLevel"] === "Manager". An Expression route with one condition works too: variable on Bucket NameGeneral, Field NameApprovalLevel, Data Type Text, Equals the static valueManager. - To the approved path: select it and choose Make Default.
- Save the workflow.
Script¶
The script writes document Index.NeedsReview (Yes or No), and work item Index.NeedsReview, Index.BatchTotal, General.ApprovalLevel (Standard, Manager or Director) and General.RoutingReason.
// Custom Step "Approval Level". After extraction, before the approval routes.
// Reads: Index.InvoiceTotal (each document), Index.VendorStatus (work item),
// global variables ManagerLimit and DirectorLimit.
// Writes: Index.NeedsReview (each document and work item), Index.BatchTotal,
// General.ApprovalLevel, General.RoutingReason (work item).
(function () {
function toNumber(text) {
var n = parseFloat(String(text || "").replace(/[^0-9.\-]/g, ""));
return isNaN(n) ? null : n;
}
var managerLimit = toNumber(GlobalVariables.Get("ManagerLimit"));
var directorLimit = toNumber(GlobalVariables.Get("DirectorLimit"));
if (managerLimit === null || directorLimit === null) {
throw new Error("ManagerLimit and DirectorLimit must be numbers");
}
var total = 0;
var flagged = 0;
for (var i = 0; i < batch.Documents.length; i++) {
var d = batch.Documents[i];
var amount = toNumber((d.Metadata["Index"] || {})["InvoiceTotal"]);
// An unreadable total needs a person too.
var needs = amount === null || amount > managerLimit;
if (amount !== null) { total += amount; }
if (needs) { flagged++; }
builder.SetDocumentMetadata(d.DocumentId, "Index", "NeedsReview", needs ? "Yes" : "No");
}
var vendorStatus = String((batch.Metadata["Index"] || {})["VendorStatus"] || "").toLowerCase();
var level = "Standard";
var reason = "All invoices within " + managerLimit.toFixed(2);
if (total > directorLimit) {
level = "Director";
reason = "Work item total " + total.toFixed(2) + " over " + directorLimit.toFixed(2);
} else if (flagged > 0) {
level = "Manager";
reason = flagged + " invoice(s) over " + managerLimit.toFixed(2) + " or unreadable";
} else if (vendorStatus === "new") {
level = "Manager";
reason = "New vendor";
}
// Routes read the work item, not its documents.
builder.SetBatchMetadata("Index", "NeedsReview", flagged > 0 ? "Yes" : "No");
builder.SetBatchMetadata("Index", "BatchTotal", total.toFixed(2));
builder.SetBatchMetadata("General", "ApprovalLevel", level);
builder.SetBatchMetadata("General", "RoutingReason", reason);
})();
How it works¶
GlobalVariables.Getreturns text, sotoNumberparses the limits. The script fails on purpose, with a clear message, if a limit is not a number. That is safer than approving everything.- Each document gets its own
Index.NeedsReview, so the approver sees which invoice was flagged. - The decision is one work-item value,
General.ApprovalLevel. Each route compares one value, and the order of the checks lives in one script, not spread over routes. General.RoutingReasonsays why, in words. Show it on the task, and use it when someone asks why an invoice skipped review.- One condition per route avoids the known issue with compound Expression routes. See Routes.
Read the values later¶
- Javascript route
(workItem.Metadata["General"] || {})["ApprovalLevel"] === "Director" - Expression route A variable with Bucket Name
General, Field NameApprovalLevel, Data Type Text. - Liquid template
{{ WorkItem.Metadata.General.ApprovalLevel }} - A later Custom Step or Custom Event step
batch.Metadata["General"]["ApprovalLevel"]
A Custom Step cannot create or change a global variable. Values for one work item belong in its metadata, which every later step can read.
Test it¶
- Upload three invoices in one work item: 3,751.02, 8,947.00 and 18,330.34.
- Open the work item in Monitor or Insight.
- Change
DirectorLimitto30000, upload the same three invoices again, and check the new work item.
You should see
on the first work item: Index.NeedsReview = Yes only on the third document, Index.BatchTotal = 31028.36, General.ApprovalLevel = Manager, and the work item in the manager task. The second work item reads the new limit and goes to the director task with General.ApprovalLevel = Director.
What can go wrong¶
Watch out
- Every work item takes the default route. The route reads document metadata, or compares
"manager"with"Manager". Text comparisons are case-sensitive. Global variable ManagerLimit was not found.The variable does not exist, or the name has a different case. Create it before the first work item reaches the step.- A new limit has no effect on running work items. Work items read the variable version from when they were submitted. Test with a new work item.
- An Expression route with two conditions joined by Or approves too little. That route type evaluates Or as And. Keep one condition per route, as here.
Related¶
- Routes
- Global Variables
- Loop Through Documents and Total the Work Item
- Your Own Endpoint: the same review-limit logic as a unit-tested Azure Function.