Custom Step Reference¶
This page lists every object and method a Custom Step script can use. Anything not on this page does not exist in the Custom Step. For the runtime contract, the run lifecycle and the rules, see Custom Step.
Batch means work item
"Batch" is the older name for a work item. Some names still say batch (batch, SetBatchMetadata). They mean the work item.
All members¶
| Member | What it does | Returns |
|---|---|---|
builder.SetBatchMetadata |
Sets a work-item metadata value | builder |
builder.SetWorkItemMetadata |
Same as SetBatchMetadata |
builder |
builder.SetDocumentMetadata |
Sets a metadata value on one document | builder |
builder.SetPageMetadata |
Sets a metadata value on one page | builder |
builder.SetBatchName |
Renames the work item | builder |
builder.SetWorkItemName |
Same as SetBatchName |
builder |
builder.SetContentType |
Sets a document's content type | builder |
builder.CreateDocument |
Adds an empty document | document id |
builder.DeleteDocument |
Removes a document and its pages | builder |
builder.DeletePage |
Removes one page | builder |
builder.CopyPageToDocument |
Copies one page to a document | builder |
builder.CopyAllDocumentPages |
Copies all pages to a document | builder |
builder.MoveDocument |
Moves a document in the work item | builder |
builder.CreateWorkItem |
Creates an empty work item | work item id |
builder.CopyDocumentToWorkItem |
Copies a document to a new work item | builder |
builder.MoveDocumentToWorkItem |
Moves a document to a new work item | builder |
builder.ConvertTableFieldToTableArray |
Parses table CSV into rows | string[][] |
builder.ConvertTableArrayToTableField |
Turns rows back into CSV | string |
builder.ApplyChanges |
Applies queued operations now | builder |
helper.GenerateUUID |
Generates a random UUID | string |
GlobalVariables.Get |
Reads a global variable | string |
Papa.parse, Papa.unparse |
Parses and writes CSV text | object, string |
Objects in scope¶
| Object | Use |
|---|---|
batch |
Read the work item. |
workItem |
A second, separate copy of the same data. Same shape as batch. |
builder |
Queue changes to the work item. |
helper |
GenerateUUID(). |
GlobalVariables |
Get(name): read a workflow global variable. |
Papa |
PapaParse 5.0.2, for CSV text. |
The work item: batch and workItem¶
Both are plain JavaScript objects parsed from the same JSON when the step starts. Changing them changes nothing on the work item. After builder.ApplyChanges(), both are reloaded.
| Property | Type and meaning |
|---|---|
batch.BatchId |
string (GUID): the work item id |
batch.BatchName |
string: the work item name |
batch.Metadata[bucket][key] |
string: work-item metadata |
batch.Documents |
array of documents, in work item order |
doc.DocumentId |
string (GUID) |
doc.Metadata[bucket][key] |
string: document metadata |
doc.Files |
array: Type ("Input", "Output" or "OCR"), Name, Extension |
doc.Pages |
array: PageId (string), Index (number), Metadata[bucket][key], OriginFileName |
Common buckets¶
- Index Indexed fields, the values users see in the index panel.
- General Other values. Examples the platform writes:
DocumentName(the document's display name),ContentTypeId,DocURL(after Upload To Destination, when the destination returns a URL). - ApiResults Written by Call External API at work-item level:
LastHttpStatusCodeand the Response Metadata Key. - Trigger Values from the trigger form that started the work item.
- Error Written when a step fails:
LastErrorMessage,LastErrorStepNameand others. - System Platform values. Read only.
A bucket that does not exist is undefined. Guard it: (batch.Metadata["Index"] || {})["Vendor"].
How builder works¶
Every builder method below adds an operation to a queue and returns builder, unless its entry says otherwise. The engine applies the queue in order when the script ends, or when you call ApplyChanges(). Ids are strings.
Metadata write rules¶
These rules apply to every Set...Metadata method:
- Pass strings. Convert numbers and booleans yourself:
amount.toFixed(2),String(count),ok ? "Yes" : "No". - A
nullorundefinedvalue is skipped without an error. - Any write to the
Systembucket (any letter case) is skipped without an error. - If you set the same key twice, the last value wins.
- An empty string
""stores an empty value. There is no method that removes a key.
Metadata and names¶
builder.SetBatchMetadata¶
Sets a work-item metadata value. Routes read this level.
builder.SetBatchMetadata(bucket, key, value)
| Parameter | Type | Description |
|---|---|---|
bucket |
string | The bucket, for example "General". |
key |
string | The key. Case-sensitive. |
value |
string | The value. |
Returns: builder.
builder.SetWorkItemMetadata¶
The same operation as SetBatchMetadata, under the newer name.
builder.SetWorkItemMetadata(bucket, key, value)
Returns: builder.
builder.SetDocumentMetadata¶
Sets a metadata value on one document.
builder.SetDocumentMetadata(documentId, bucket, key, value)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | A DocumentId from batch.Documents, or an id that CreateDocument() returned. |
bucket, key, value |
string | As for SetBatchMetadata. |
Returns: builder.
builder.SetPageMetadata¶
Sets a metadata value on one page.
builder.SetPageMetadata(documentId, pageId, bucket, key, value)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | The document that holds the page. |
pageId |
string | A PageId from doc.Pages. |
bucket, key, value |
string | As for SetBatchMetadata. |
Returns: builder.
builder.SetBatchName¶
Renames the work item. The engine applies the name after all other operations. The last call wins.
builder.SetBatchName(name)
| Parameter | Type | Description |
|---|---|---|
name |
string | The new name. |
Returns: builder.
builder.SetWorkItemName¶
The same operation as SetBatchName, under the newer name.
builder.SetWorkItemName(name)
Returns: builder.
builder.SetContentType¶
Sets the document's content type. Writes General.ContentTypeId and General.DocTypeId.
builder.SetContentType(documentId, contentTypeId)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | The document. |
contentTypeId |
string | The content type's GUID, not its display name. |
- Returns
builder - Fails The step fails with
Failed to Set Content Type on Document ...if no content type has that id.
Documents and pages¶
builder.CreateDocument¶
Adds an empty document to this work item.
builder.CreateDocument()
Returns: string, the new document's id, at once. Use it in later calls of the same script.
var newDocId = builder.CreateDocument();
builder.CopyPageToDocument(source.DocumentId, source.Pages[0].PageId, newDocId);
builder.DeleteDocument¶
Removes the document and its pages.
builder.DeleteDocument(documentId)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | The document to remove. |
Returns: builder.
builder.DeletePage¶
Removes one page from a document.
builder.DeletePage(documentId, pageId)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | The document. |
pageId |
string | The page to remove. |
Returns: builder.
builder.CopyPageToDocument¶
Copies one page to the end of the target document, or to the start when prepend is true.
builder.CopyPageToDocument(sourceDocumentId, pageId, targetDocumentId, prepend = false)
| Parameter | Type | Description |
|---|---|---|
sourceDocumentId |
string | The document that holds the page. |
pageId |
string | The page to copy. |
targetDocumentId |
string | The document that receives the copy. |
prepend |
boolean, optional | true to put the page first. Default false. |
Returns: builder.
builder.CopyAllDocumentPages¶
Copies all pages of the source, in order, to the end or the start of the target.
builder.CopyAllDocumentPages(sourceDocumentId, targetDocumentId, prepend = false)
| Parameter | Type | Description |
|---|---|---|
sourceDocumentId, targetDocumentId |
string | The two documents. |
prepend |
boolean, optional | true to put the pages first. Default false. |
Returns: builder.
builder.CopyAllDocumentPages(attachment.DocumentId, main.DocumentId);
builder.DeleteDocument(attachment.DocumentId);
builder.MoveDocument¶
Moves a document within the work item.
builder.MoveDocument(documentId, afterDocumentId = null)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | The document to move. |
afterDocumentId |
string or null, optional |
The document to place it after. null moves it to the front. |
Returns: builder.
New work items¶
builder.CreateWorkItem¶
Creates an empty work item with that name. It has no work-item metadata.
builder.CreateWorkItem(name)
| Parameter | Type | Description |
|---|---|---|
name |
string | The new work item's name. |
Returns: string, the new work item's id, at once.
When the step succeeds, the new work item is saved, linked to this work item, and follows this step's routes, evaluated on its own metadata. Give the step a route or a default route that new work items can take.
builder.CopyDocumentToWorkItem¶
Copies a document, with its pages and document metadata, to a new work item.
builder.CopyDocumentToWorkItem(documentId, targetWorkItemId)
| Parameter | Type | Description |
|---|---|---|
documentId |
string | The document to copy. |
targetWorkItemId |
string | An id that CreateWorkItem returned in this run. Any other id, including an existing work item, fails the step with Target work item does not exist. |
Returns: builder.
builder.MoveDocumentToWorkItem¶
Same as CopyDocumentToWorkItem, then removes the document from this work item. The same rule applies to targetWorkItemId.
builder.MoveDocumentToWorkItem(documentId, targetWorkItemId)
Returns: builder.
No documents into an existing work item
A Custom Step cannot add documents to an existing work item. Use your own endpoint with V1_KWorkItemAPI/AddDocument for that.
Table fields¶
A table field's value is CSV text. In most tables the first row holds the column names.
builder.ConvertTableFieldToTableArray¶
Parses the CSV text into rows of cells (with PapaParse). Runs at once; it is not queued.
builder.ConvertTableFieldToTableArray(tableString)
| Parameter | Type | Description |
|---|---|---|
tableString |
string | The table field's value. |
- Returns
string[][] - Empty rows Parsing can return a last row with one empty cell. Skip empty rows.
- Fails The step error says
Function ConvertTableFieldToTableArray failed to parse value into TableArray.
builder.ConvertTableArrayToTableField¶
Turns rows of cells back into CSV text. Runs at once. Write the result with SetDocumentMetadata.
builder.ConvertTableArrayToTableField(array)
| Parameter | Type | Description |
|---|---|---|
array |
string[][] |
The rows. |
Returns: string.
builder.SetDocumentMetadata(doc.DocumentId, "Index", "LineItems", builder.ConvertTableArrayToTableField(rows));
Applying changes early¶
builder.ApplyChanges¶
Applies the queued operations now, reloads batch and workItem with the result, and empties the queue. Does nothing when the queue is empty.
builder.ApplyChanges()
Returns: builder.
- Use it when a later part of the script must see an earlier change, for example the pages of a document you created earlier in the script.
- It counts against the step timeout. A timeout inside it gives
Javascript timeout limit was exceeded while ApplyingChanges. - Variables that point into the old
batch(for examplevar docs = batch.Documents) still point at the old data. Readbatchagain after the call. - The changes are saved only when the whole script succeeds.
Do not call builder.Build()
It is internal.
helper¶
helper.GenerateUUID¶
Generates a random version-4-style UUID. It uses Math.random, so it is fine for ids and not for security tokens.
helper.GenerateUUID()
Returns: string, such as 3f2b9c1a-....
GlobalVariables¶
GlobalVariables.Get¶
Reads a workflow global variable, in the version current when the work item was last submitted, or when it entered the sub-workflow.
GlobalVariables.Get(name)
| Parameter | Type | Description |
|---|---|---|
name |
string | The variable name. Case-sensitive. |
- Returns string. This includes the value of a variable marked secret: never write it to metadata.
- Throws
Global variable {name} was not found.when the variable does not exist. The step fails unless you catch the error.
Papa¶
Papa.parse, Papa.unparse¶
PapaParse 5.0.2 is loaded. Use it for CSV text in metadata.
Papa.parse(text, config) and Papa.unparse(data)
| Parameter | Type | Description |
|---|---|---|
text |
string | The CSV text. |
config |
object | PapaParse options, for example { header: true, skipEmptyLines: true }. |
data |
array | Rows to turn back into CSV. |
Returns: Papa.parse returns { data, errors, meta }. Papa.unparse returns CSV text.
var rows = Papa.parse(csvText, { header: true, skipEmptyLines: true }).data;
var text = Papa.unparse(rows);
Errors¶
Error text in Error.LastErrorMessage |
Cause |
|---|---|
Error in executing Javascript V8. Javascript timeout limit was exceeded |
The script ran past the timeout. |
...Javascript timeout limit was exceeded while ApplyingChanges. |
The timeout was reached inside ApplyChanges(). |
Error in executing Javascript V8. followed by the exception |
Your script threw, or a syntax error. The text includes the message and line. |
Global variable {name} was not found. |
GlobalVariables.Get got a name that does not exist. |
Target work item does not exist. |
See New work items. |
Failed to Set Content Type on Document ... |
SetContentType got an id that matches no content type. |
To fail the step on purpose, throw an Error: throw new Error("No invoice total on document " + d.DocumentId);
Reserved names¶
Do not declare or reassign these
The engine defines builder, helper and operations as constants. Declaring a variable with one of these names is a syntax error. Do not reassign batch, workItem, initializeBatch, GlobalVariables or Papa.
What does not exist¶
These are not available in a Custom Step. Code that uses them fails with a ReferenceError or a syntax error:
console,logger: there is no log. Write diagnostics to metadata.http,fetch,XMLHttpRequest,WebSocket: there is no network. Use Call External API.require,import, modules, the file system.setTimeout,setInterval.- Top-level
awaitand top-levelreturn. - Methods that write global variables. A Custom Step can only read them.
- The Process Designer signatures
SetDocumentMetadata(bucket, key, value)andSetPageMetadata(bucket, key, value).