Split a Scanned Work Item into One Document per Page¶
A mailroom scans a stack of single-page forms as one document. Each page is its own form and must be classified on its own. The workflow splits the scan so every page becomes its own work item with one document, and sends each new work item to a second workflow for classification. The original ends.
At a glance
- Extension point Custom Step with routes and a Send To Workflow step
- You need A work item with one scanned document, with optional
Indexvalues to copy to each page; a classification workflow; permission to edit both workflows. - Teaches
CreateDocument,CopyPageToDocument,CreateWorkItem,MoveDocumentToWorkItem, and how new work items follow the step's routes.
Set it up¶
- In the Workflow designer, add a Custom Step named "Split Pages" with the script below.
- Add a final step for the original work item, with no outgoing routes, for example a Custom Step that writes
General.SplitDone. - From "Split Pages", draw a Javascript route to that final step:
(workItem.Metadata["General"] || {})["SplitRole"] === "Parent" - From "Split Pages", draw a route to a Send To Workflow step set to the classification workflow, and make it the default route.
- Save the workflow.
Script¶
The script writes, on each new document, the same Index values as the scan plus General.SourcePage. On the original work item, it writes General.SplitRole and General.SplitCount.
// Custom Step "Split Pages". Start of the mailroom workflow.
// Reads: the work item's single document, its pages and its Index values.
// Writes: one new work item per page, each with one document (Index values + General.SourcePage);
// General.SplitRole and General.SplitCount on the original.
(function () {
if (batch.Documents.length !== 1) {
throw new Error("Split Pages expects one scanned document, found " + batch.Documents.length);
}
var source = batch.Documents[0];
var sourceIndex = source.Metadata["Index"] || {};
// Copy the page list and sort it, so the order never depends on the array.
var pages = source.Pages.slice().sort(function (a, b) { return a.Index - b.Index; });
if (pages.length < 2) {
builder.SetBatchMetadata("General", "SplitRole", "Single");
return;
}
for (var p = 0; p < pages.length; p++) {
// 1. A new one-page document in this work item.
var newDocId = builder.CreateDocument();
builder.CopyPageToDocument(source.DocumentId, pages[p].PageId, newDocId);
// 2. Carry the index values over and note the page number.
for (var key in sourceIndex) {
builder.SetDocumentMetadata(newDocId, "Index", key, sourceIndex[key]);
}
builder.SetDocumentMetadata(newDocId, "General", "SourcePage", String(p + 1));
// 3. A new work item for it.
var childId = builder.CreateWorkItem(batch.BatchName + " - page " + (p + 1));
builder.MoveDocumentToWorkItem(newDocId, childId);
}
// The original keeps no documents and is marked so its route can end it.
builder.DeleteDocument(source.DocumentId);
builder.SetBatchMetadata("General", "SplitRole", "Parent");
builder.SetBatchMetadata("General", "SplitCount", String(pages.length));
})();
How it works¶
CreateDocument()returns the new document's id at once, so later calls in the same script can use it.- The engine applies the operations in order when the script ends: create the document, copy the page, write metadata, create the work item, move the document.
MoveDocumentToWorkItemaccepts only a work item thatCreateWorkItemmade in the same run.- When the step succeeds, each new work item is linked to the original and then follows this step's routes, evaluated on its own metadata. A new work item has no work-item metadata, so it does not match the
Parentroute and takes the default route to Send To Workflow. - A single-page scan (
SplitRole=Single) also takes the default route and is classified as it is.
Test it¶
- Upload a three-page PDF as one document on a non-production tenant.
- In Monitor or Insight, open the original work item, then look for the new work items in the classification workflow.
You should see
the original work item end with General.SplitRole = Parent and General.SplitCount = 3, and three new work items named ... - page 1 to ... - page 3 in the classification workflow, each with one one-page document and General.SourcePage set.
What can go wrong¶
Watch out
- "Split Pages expects one scanned document". An earlier step already separated the scan. Run the split before separation, or loop over every document.
- "Target work item does not exist." The script passed an id that
CreateWorkItemdid not return in this run. - The step fails, or the new work items do not move on. The step has no default route and no route they match. Add the default route.
- The step fails on a very large scan. Each page adds operations. Test with your largest expected document, and keep the script under the 120-second timeout.
- An older script from this site does not work. Old split examples used Process Designer objects. Use this one.
Variations¶
One document per page, same work item. Drop step 3 of the loop. The work item then holds one document per page, the original document is deleted, and no new work items are created. Also remove the line that sets SplitRole to Parent: otherwise this work item takes the Parent route, not the route to classification.