Skip to content

Name Documents and Generate Reference Numbers

A shared services team finds work items in Monitor by name, quotes a reference number to vendors on the phone, and sends a globally unique id to its ERP. Scanned work items arrive with names like Scan_0931 and documents named Document 1. One Custom Step after extraction gives each document a readable name, a reference number and a UUID, and gives the work item a name people can search for.

At a glance

  • Extension point Custom Step
  • You need Document fields Index.Vendor, Index.InvoiceNumber and optionally Index.DocumentType; a field ReferenceNumber on the content type if it must reach the destination; permission to edit the workflow.
  • Teaches General.DocumentName, builder.SetWorkItemName, helper.GenerateUUID(), and values that do not change when the step runs again.

Set it up

  1. In the Workflow designer, add a Custom Step after extraction and before the step that uploads or exports the documents. Name it "Names and References".
  2. Paste the script below into the JavaScript Block.
  3. Add ReferenceNumber to the content type if the destination should receive it.
  4. Save the step and the workflow.

Script

The script writes document General.DocumentName (the display name), Index.ReferenceNumber and General.ExternalId, work item General.ReferencePrefix, and renames the work item.

Custom Step: Names and References
// Custom Step "Names and References". After extraction, before upload.
// Reads: Index.Vendor, Index.InvoiceNumber, Index.DocumentType, Index.ReferenceNumber,
//        General.ExternalId (each document); General.ReferencePrefix (work item).
// Writes: General.DocumentName, Index.ReferenceNumber, General.ExternalId (each document);
//         General.ReferencePrefix and the work item name.
(function () {
  function clean(text, maxLength) {
    return String(text || "")
      .replace(/[\\\/:*?"<>|]/g, "")   // characters that file systems reject
      .replace(/\s+/g, " ")
      .trim()
      .substring(0, maxLength);
  }

  function pad(n, width) {
    var s = String(n);
    while (s.length < width) { s = "0" + s; }
    return s;
  }

  var now = new Date();
  var today = now.getFullYear() + "-" + pad(now.getMonth() + 1, 2) + "-" + pad(now.getDate(), 2);

  // Keep the prefix from the first run, so reference numbers never change.
  var prefix = (batch.Metadata["General"] || {})["ReferencePrefix"];
  if (!prefix) {
    // The date plus the first 8 characters of the work item id: unique per work item.
    prefix = today.replace(/-/g, "") + "-" + String(batch.BatchId).replace(/-/g, "").substring(0, 8).toUpperCase();
    builder.SetBatchMetadata("General", "ReferencePrefix", prefix);
  }

  var vendors = {};

  for (var i = 0; i < batch.Documents.length; i++) {
    var doc = batch.Documents[i];
    var index = doc.Metadata["Index"] || {};
    var general = doc.Metadata["General"] || {};
    var vendor = clean(index["Vendor"], 60);
    var invoice = clean(index["InvoiceNumber"], 40);
    if (vendor) { vendors[vendor] = true; }

    // 1. A display name people can read.
    var name = vendor && invoice ? vendor + " - " + invoice
             : vendor ? vendor + " - document " + (i + 1)
             : "Document " + (i + 1);
    builder.SetDocumentMetadata(doc.DocumentId, "General", "DocumentName", name);

    // 2. A reference number people can quote, for example INV-20261007-3F2B9C1A-001.
    if (!index["ReferenceNumber"]) {
      var type = String(index["DocumentType"] || "DOC").toUpperCase().replace(/[^A-Z]/g, "").substring(0, 3) || "DOC";
      builder.SetDocumentMetadata(doc.DocumentId, "Index", "ReferenceNumber", type + "-" + prefix + "-" + pad(i + 1, 3));
    }

    // 3. A globally unique id for integrations.
    if (!general["ExternalId"]) {
      builder.SetDocumentMetadata(doc.DocumentId, "General", "ExternalId", helper.GenerateUUID());
    }
  }

  var vendorList = Object.keys(vendors);
  var label = vendorList.length === 1 ? vendorList[0]
            : vendorList.length > 1 ? "Mixed vendors"
            : "Unknown vendor";

  builder.SetWorkItemName(label + " - " + today + " - " + batch.Documents.length + " docs");
})();

How it works

  • General.DocumentName holds the name Monitor and the Index app show for a document. clean removes characters that break file and folder names, in case a later step builds a file name from the same values.
  • builder.SetWorkItemName (the same as SetBatchName) renames the work item. The engine applies the name after all other operations. The last call wins.
  • The reference number combines the document type, the date, part of the work item id and the document's position. batch.BatchId is unique per work item, and the position is unique inside it.
  • The reference number and the UUID are written only when they are empty, and the prefix is kept from the first run. A retry or a loop back to this step keeps the numbers people already quoted.
  • helper.GenerateUUID() uses Math.random: fine for ids, not for secrets. The document's own DocumentId is already a GUID. Use it when a downstream system can store it.

Test it

  1. Upload a work item with two invoices from the same vendor.
  2. Open it in Monitor or Insight.
  3. Send the same work item through the step again (for example by routing back), and check the values again.

You should see

the work item named <Vendor> - <date> - 2 docs, documents named <Vendor> - <invoice number>, reference numbers ending in -001 and -002, and two different General.ExternalId values. After the second pass, the reference numbers and ids are unchanged.

What can go wrong

Watch out

  • Names show Document 1. Extraction did not fill Vendor or InvoiceNumber, or the keys differ in case. Check the document's Index metadata.
  • The uploaded file name did not change. General.DocumentName is a display name in KnowledgeLake. The file name in a destination comes from the upload or move settings.
  • The reference number does not reach the destination. ReferenceNumber must exist as a field on the content type for upload to map it.
  • Two documents get the same name. Two invoices share a number. Add the position (i + 1) when uniqueness matters.
  • You need a gap-free running number such as INV-000123. A Custom Step cannot keep a counter across work items: it cannot write global variables and has no database. Let the receiving system assign it, or call your own endpoint that issues numbers.