Skip to content

Copy a Document into Another Work Item

A loan file arrives as one work item with many documents. Contracts must also go to a legal review workflow, but the loan file must keep them too. The Custom Step copies every contract into a new work item that goes to legal, and the loan file continues with all its documents.

At a glance

  • Extension point Custom Step with routes and a Send To Workflow step
  • You need Document field Index.DocumentType (documents with Contract are copied); a legal review workflow; permission to edit both workflows.
  • Teaches CreateWorkItem with CopyDocumentToWorkItem, routing the original and the copy apart, and why "Target work item does not exist" happens.

Set it up

  1. In the Workflow designer, add a Custom Step named "Copy Contracts" with the script below.
  2. From "Copy Contracts", draw a Javascript route to the next loan step: (workItem.Metadata["General"] || {})["CopyRole"] === "Source"
  3. From "Copy Contracts", draw a route to a Send To Workflow step set to the legal review workflow, and make it the default route. Only the new work item takes it.
  4. Save the workflow.

Script

The script writes work item General.CopyRole = Source and General.ContractsCopied on the original.

Custom Step: Copy Contracts
// Custom Step "Copy Contracts". In the loan workflow.
// Reads: Index.DocumentType (each document).
// Writes: a new work item "Contracts - <name>" with a copy of each contract;
//         General.CopyRole = "Source" and General.ContractsCopied on the original.
(function () {
  var contracts = [];
  for (var i = 0; i < batch.Documents.length; i++) {
    var type = ((batch.Documents[i].Metadata["Index"] || {})["DocumentType"] || "").toLowerCase();
    if (type === "contract") {
      contracts.push(batch.Documents[i].DocumentId);
    }
  }

  builder.SetBatchMetadata("General", "CopyRole", "Source");
  builder.SetBatchMetadata("General", "ContractsCopied", String(contracts.length));

  if (contracts.length === 0) {
    return;
  }

  var legalId = builder.CreateWorkItem("Contracts - " + batch.BatchName);
  for (var c = 0; c < contracts.length; c++) {
    builder.CopyDocumentToWorkItem(contracts[c], legalId);
  }
})();

How it works

  • The script collects the contract ids first, then acts. It creates one new work item only when there is something to copy.
  • CreateWorkItem returns the new id at once. CopyDocumentToWorkItem copies each contract with its pages and document metadata.
  • When the step succeeds, both the original and the new work item leave "Copy Contracts" along its routes. The original has General.CopyRole = Source and takes the loan route. The new work item has no work-item metadata, so it takes the default route to legal.
  • To take the documents out of the loan file instead of copying them, use builder.MoveDocumentToWorkItem(documentId, legalId).

Why "Target work item does not exist" happens

CopyDocumentToWorkItem and MoveDocumentToWorkItem accept only an id that builder.CreateWorkItem returned in the same run of the script. Any other id, including the id of an existing work item, fails the step with:

Target work item does not exist.

So a Custom Step cannot add a document to a work item that already exists, for example one waiting on hold in another workflow. To do that, use your own endpoint: it downloads the file with V1_KWorkItemAPI/GetDocumentFile and adds it to the target with V1_KWorkItemAPI/AddDocument. See Your Own Endpoint.

Test it

  1. Upload a work item with two contracts and one other document on a non-production tenant.
  2. In Monitor or Insight, open the original work item, then find the new work item in the legal workflow.

You should see

the original continue on the loan path with all three documents and General.ContractsCopied = 2, and a new work item named Contracts - ... with the two contracts in the legal workflow.

What can go wrong

Watch out

  • "Target work item does not exist." See above.
  • The step fails, or the new work item does not move on. The step has no default route and no route the new work item matches. Add the default route.
  • The original goes to legal too. The route condition does not match the original, for example because CopyRole has a different case. Check General.CopyRole on the original.
  • No contracts are copied. Index.DocumentType holds a different value, such as Agreement. Check the values on real documents.