Skip to content

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 field Index.VendorStatus (for example New or Approved); global variables ManagerLimit and DirectorLimit (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

  1. In the Workflow app, open Global Variables. Add ManagerLimit (Generic Value, Decimal, 10000) and DirectorLimit (Decimal, 50000).
  2. In the Workflow designer, add a Custom Step after extraction. Name it "Approval Level". Paste the script below.
  3. From "Approval Level", draw three routes:
  4. To the director task, a Javascript route: (workItem.Metadata["General"] || {})["ApprovalLevel"] === "Director"
  5. To the manager task, a Javascript route: (workItem.Metadata["General"] || {})["ApprovalLevel"] === "Manager". An Expression route with one condition works too: variable on Bucket Name General, Field Name ApprovalLevel, Data Type Text, Equals the static value Manager.
  6. To the approved path: select it and choose Make Default.
  7. 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
// 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.Get returns text, so toNumber parses 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.RoutingReason says 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 Name ApprovalLevel, 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

  1. Upload three invoices in one work item: 3,751.02, 8,947.00 and 18,330.34.
  2. Open the work item in Monitor or Insight.
  3. Change DirectorLimit to 30000, 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.