Skip to content

Debugging Custom Step Scripts

This page helps you find out why a Custom Step failed, or why it ran but did not do what you expected. It also covers the Custom Event step, which runs the same kind of script. Each section starts from what you see. For the objects and methods, see the Custom Step Reference.

Where to look first

  1. Open the work item in Monitor or Insight.
  2. Read work-item Error.LastErrorMessage, Error.LastErrorStepName and Error.LastErrorDateTime. For a script error the message starts with Error in executing Javascript V8. and includes the exception and the line.
  3. Read the metadata the script should have written, at the level it should have written it (work item or document).

There is no console in a Custom Step. To see what a script decided, write a trace to metadata (see Handle Errors and Leave a Trace). A trace is saved only when the script succeeds; when it throws, put the key facts in the error message.

Summary

Symptom Group
console is not defined Script errors
Global variable not found Script errors
Illegal return statement Script errors
builder has already been declared Script errors
Cannot read properties of undefined Script errors
Script timed out Script errors
Table field could not be parsed Script errors
Target work item does not exist Script errors
Content type could not be set Script errors
The step succeeded, but a value is missing Wrong result
A number shows as NaN, or every amount is 0 Wrong result
A value I set reads back as the old value Wrong result
Every work item takes the default route Wrong result
Some documents were skipped or handled twice Wrong result
The step succeeds but no event arrives Custom Event step
The step fails right after SetEventProperty Custom Event step

Script errors

console is not defined

  • Message ReferenceError: console is not defined
  • Cause The script calls console.log, or another object that does not exist in the Custom Step: logger, http, fetch, XMLHttpRequest, require, setTimeout.
  • Fix Remove the call. Write diagnostics to a General key, collect notes in an array and write them once at the end, or throw an Error to stop with a message.
  • Network calls Use Call External API.
A trace instead of console
var trace = [];
trace.push("documents=" + batch.Documents.length);
// ... your logic, adding notes ...
builder.SetBatchMetadata("General", "StepTrace", trace.join(" | ").substring(0, 2000));

Global variable not found

  • Message Global variable ReviewLimit was not found.
  • Cause GlobalVariables.Get got a name that does not exist. Names are case-sensitive. This is the message in Custom Steps, Custom Event steps, routes and preprocessors.
  • Fix Create the variable, or copy its exact name from the Global Variables list.
  • Optional variable Wrap the call in try/catch. GlobalVariables.Get("X") || "default" does not help, because the call throws first.
  • See Global Variables

Illegal return statement

  • Message SyntaxError: Illegal return statement
  • Cause A return at the top level of the script. The script runs as a classic script, not inside a function.
  • Fix Wrap the script in a function you call at once: (function () { ... return; ... })();

builder has already been declared

  • Message Identifier 'builder' has already been declared
  • Cause The script declares builder, helper or operations. The engine defines these as constants.
  • Fix Rename your variable.

Cannot read properties of undefined

  • Message TypeError: Cannot read properties of undefined
  • Cause A missing bucket, for example doc.Metadata["Index"]["Total"] on a document with no Index bucket.
  • Fix Read the bucket with an empty-object fallback first, then the key: (doc.Metadata["Index"] || {})["Total"].

Script timed out

  • Message Error in executing Javascript V8. Javascript timeout limit was exceeded
  • Variant The message ends with while ApplyingChanges. when the time ran out inside builder.ApplyChanges().
  • Cause The script ran past the step timeout: 120 seconds by default for a Custom Step (KnowledgeLake can change it per tenant), 10 seconds for a Custom Event step.
  • Fix Avoid nested loops over all documents and all pages when one pass will do.
  • Lookups Build a lookup object once (var byNumber = {}) instead of searching an array inside a loop.
  • ApplyChanges Call builder.ApplyChanges() once, not inside a loop.
  • Heavy work Move it to your own endpoint.

Table field could not be parsed

  • Message Function ConvertTableFieldToTableArray failed to parse value into TableArray
  • Cause builder.ConvertTableFieldToTableArray got text that is not CSV.
  • Fix Check that the key is the table field, and that the value is not empty.
  • See Work With Line-Item Tables

Target work item does not exist

  • Message Target work item does not exist.
  • Cause CopyDocumentToWorkItem or MoveDocumentToWorkItem got an id that builder.CreateWorkItem did not return in this run, for example the id of an existing work item.
  • Fix Create the target in the same script. To add a document to an existing work item, use your own endpoint with V1_KWorkItemAPI/AddDocument.
  • See Copy a Document into Another Work Item

Content type could not be set

  • Message Failed to Set Content Type on Document ...
  • Cause builder.SetContentType got an id that matches no content type, often a display name instead of the GUID.
  • Fix Pass the content type's GUID. Keep it in a global variable rather than in the script.

Wrong result

The step succeeded, but a value is missing

  • Cause One of the three causes below.
  • System bucket The value was written to the System bucket. The engine skips writes to System with no error.
  • Empty value The value was null or undefined, so the write was skipped.
  • Key case The key has a different case from the one you look for.
  • Fix Write your own values to General. Check the variable before you write; write "" on purpose if you want an empty value. Copy key names from the content type.

A number shows as NaN, or every amount is 0

  • Cause The source had $ or ,, or the key is wrong, so parseFloat read nothing.
  • Fix Strip non-numeric characters first: parseFloat(String(x || "").replace(/[^0-9.\-]/g, "")). Check the key's case.

A value I set reads back as the old value

  • Cause builder calls are queued and apply when the script ends. batch does not change during the run.
  • Fix Use the local variable you computed. If a later part of the script must see the change in batch, call builder.ApplyChanges() first, then read batch again.

Every work item takes the default route

  • Cause The script set the value per document. Routes read work-item metadata (Expression routes read nothing else).
  • Fix Also write the value with builder.SetBatchMetadata.
  • See Routes

Some documents were skipped or handled twice

  • Cause builder.ApplyChanges() ran inside a loop over batch.Documents and reloaded batch, or the script kept an old reference such as var docs = batch.Documents after the call.
  • Fix Collect the ids first, act second, and call ApplyChanges() once, after the loop. Read batch again after the call.

Custom Event step

The step succeeds but no event arrives

  • Cause The tenant has no Secondary Event Provider, or the provider's Filter Events list has Custom unticked. Delivery happens in the background, so the workflow shows no error.
  • Fix Open Configuration > Apps > Event Settings, edit the provider, and tick Custom.
  • See Custom Event Step

The step fails right after SetEventProperty

  • Cause A property value was an object or an array. Event properties are strings only.
  • Fix Send structures with JSON.stringify(value), and numbers with String(n) or n.toFixed(2).

Test safely

  1. Copy the workflow and test there, not in production.
  2. Start with one work item and one document.
  3. Write a trace key while you develop. Remove it when the step is stable.
  4. Check the result in Monitor or Insight after each change: the metadata, the step error, and the route taken.