Skip to content

Validation Exception Event

An accounts payable team sends invoices that fail validation (no PO number, a total that does not add up) to a review task. The team lead wants an alert, outside KnowledgeLake, when exceptions pile up for one vendor. A Custom Event step on the exception path records why each work item failed. An alert rule on the team's own event store does the rest.

At a glance

  • Extension point Custom Event step
  • You need A Secondary Event Provider with Custom ticked; a validation Custom Step that writes General.ValidationStatus, General.InvalidCount and per-document General.ValidationErrors (see Validate Invoices and Send Exceptions to Review); permission to edit the workflow.
  • Teaches Placing an event on a route, choosing safe scope words, and sending a structured value with JSON.stringify.

Set it up

  1. Build the validation first. The Custom Step in Validate Invoices and Send Exceptions to Review writes the values this event reads, and its route sends invalid work items to a review task.
  2. On the route from the validation step to the review task, add a Custom Event step before the task. Name it "Validation Exception Event".
  3. Set Scopes to exception, validation. Do not use the word error: it marks the event as a platform error and also copies it into the platform's own event store.
  4. Paste the script below into JavaScript Block. Save the step and the workflow.

Script

Custom Event: Validation Exception Event
// Custom Event "Validation Exception Event". Scopes: exception, validation
// Place on the route from the validation step to the review task.
// Reads work item General.ValidationStatus, General.InvalidCount,
// and per document Index.Vendor, Index.InvoiceNumber, Index.PONumber, General.ValidationErrors.
// Writes one event: custom, exception, validation.
(function () {
  var general = batch.Metadata["General"] || {};
  var problems = [];
  var vendors = {};

  for (var i = 0; i < batch.Documents.length; i++) {
    var doc = batch.Documents[i];
    var idx = doc.Metadata["Index"] || {};
    var docGeneral = doc.Metadata["General"] || {};
    var errors = docGeneral["ValidationErrors"] || "";

    if (idx["Vendor"]) { vendors[idx["Vendor"]] = true; }
    if (!errors) { continue; }

    problems.push({
      document: i + 1,
      invoiceNumber: idx["InvoiceNumber"] || "",
      poNumber: idx["PONumber"] || "",
      errors: errors.substring(0, 300)
    });
  }

  var vendorList = Object.keys(vendors);

  builder
    .SetEventProperty("status", general["ValidationStatus"] || "")
    .SetEventProperty("invalidCount", general["InvalidCount"] || String(problems.length))
    .SetEventProperty("vendor", vendorList.length === 1 ? vendorList[0] : (vendorList.length > 1 ? "Mixed vendors" : ""))
    .SetEventProperty("workItemName", batch.BatchName || "")
    .SetEventProperty("problems", JSON.stringify(problems.slice(0, 20)));
})();

How it works

  • The step sits on the exception route, so it fires only for work items that failed. The route makes the decision; the event records it.
  • Each document's General.ValidationErrors holds the reasons the validation step found. The script collects them into a list.
  • problems is a list, so the script sends it with JSON.stringify. An object or array value would fail the step.
  • The list is capped at 20 entries and 300 characters per entry, to keep the event small.
  • vendor is the one vendor on the work item, Mixed vendors when there are several, or "". The alert groups on it.
  • The scopes exception, validation keep the event's status normal. Your alert rule decides what is urgent.

The event document

The event the alert rule reads (ids shortened):

Event document
{
  "EventId": "7f20c3d8-...",
  "Scopes": ["custom", "exception", "validation"],
  "Timestamp": "2026-10-07T15:02:44Z",
  "WorkItemId": "c5d8e2a0-...",
  "ProcessId": "2f6b11e0-...",
  "StepId": "a4e9d1b2-...",
  "WorkType": "Custom Event",
  "Properties": {
    "status": "Invalid",
    "invalidCount": "2",
    "vendor": "Gateway Fleet Services LLC",
    "workItemName": "AP scan 2026-10-07 0931",
    "problems": "[{\"document\":1,\"invoiceNumber\":\"GF-1182\",\"poNumber\":\"\",\"errors\":\"PO number is required\"},{\"document\":3,\"invoiceNumber\":\"GF-1190\",\"poNumber\":\"N/A\",\"errors\":\"PO number is required\"}]"
  }
}

Alert query

A Cosmos DB query an alert job can run every 15 minutes: vendors with three or more exceptions in the last hour.

Cosmos DB SQL: exceptions per vendor in the last hour
SELECT c.Properties.vendor AS vendor, COUNT(1) AS exceptions
FROM c
WHERE c.Scopes[0] = "custom" AND c.Scopes[1] = "exception" AND c.Scopes[2] = "validation"
  AND c.OccurredTime > @sinceMs
GROUP BY c.Properties.vendor

Pass @sinceMs as the current Unix time in milliseconds minus 3,600,000, and alert on rows with exceptions of 3 or more.

KnowledgeLake does not send alerts from these events. Build the alert on your store, with one of these:

  • a scheduled job
  • an Azure Function on the Cosmos DB change feed
  • an Elastic watcher

Test it

  1. On a non-production tenant, upload a work item with one invoice that has no PO number.
  2. Check that the work item reaches the review task, and that the document's General.ValidationErrors names the problem (Monitor or Insight).
  3. In your store, find the event by WorkItemId.
  4. Upload a clean invoice and check that no exception event is written for it.

You should see

one custom, exception, validation event for the failing work item, with invalidCount "1" and a problems string that parses as JSON, and no event for the clean work item.

What can go wrong

Watch out

  • Every work item writes an event. The step is on the shared path, not on the exception route. Move it after the route to the review task.
  • problems is []. The validation wrote its reasons somewhere else, for example to Index instead of General, or at work-item level. Match the bucket and level the validation step writes.
  • The step fails with a conversion error. A property value was the list itself. Keep JSON.stringify.
  • The same work item alerts twice. A reviewer fixed the invoice and sent it back through validation, and it failed again. That is a real second exception. Count distinct WorkItemId if you want work items, not attempts.
  • The alert never fires. The event store has the events but the job filters on Scopes[1] = "error". Match the scopes you set.