Prepare Fields Before Writing to a Data Source¶
After documents are uploaded to the document library, an accounts payable workflow writes one row per document to an ERP table with the Insert Into Data Source step. The row needs values that are not index fields: the work item name, the work item and document ids, the content type's name, and the link to the uploaded document. The step's mapping reads metadata keys, so a Custom Step copies those values into keys it can map.
At a glance
- Extension point Custom Step, between Upload To Destination and Insert Into Data Source
- You need A global variable
ContentTypeNames(JSON) that maps content type ids to names; an upload destination that returns a document URL; permission to edit the workflow. - Teaches Where the platform keeps ids, content types and document links, and mapping document-level keys into a data source row.
What exists before the script runs¶
batch.BatchNameandbatch.BatchId: the work item's name and id.doc.DocumentId: the document's id.General.ContentTypeIdon each document: the content type's id (a GUID). The content type's display name is not reliably in work item metadata, so the script maps ids to names with a global variable.General.DocURLon each document: the link to the uploaded document. Upload To Destination writes it only when the destination returns a URL, and only after the upload. If your destination returns none, the key is absent.
Set it up¶
- In the Workflow app, open Global Variables. Add
ContentTypeNames, Generic Value, data type JSON, for example{"6f1c2d3e-0000-4000-8000-000000000001": "Vendor Invoice"}. Write the ids in lowercase. - In the Workflow designer, put the steps in this order: Upload To Destination, a Custom Step named "Prepare ERP Fields" with the script below, then Insert Into Data Source.
- In Insert Into Data Source, map each ERP column to metadata: bucket
General, the key the script writes, at document level. For exampleWORK_ITEM_NAMEfromGeneral.ErpWorkItemName,DOC_LINKfromGeneral.ErpDocumentUrl. - If the ERP's link column is required, route work items with missing links to a review step before the insert, with the Javascript condition
(workItem.Metadata["General"] || {})["ErpMissingUrlCount"] !== "0". - Save the workflow.
Script¶
The script writes document General.ErpWorkItemName, General.ErpWorkItemId, General.ErpDocumentId, General.ErpContentType, General.ErpDocumentUrl, and work item General.ErpMissingUrlCount.
// Custom Step "Prepare ERP Fields". After Upload To Destination, before Insert Into Data Source.
// Reads: General.ContentTypeId, General.DocURL (each document); global variable ContentTypeNames (JSON).
// Writes: General.ErpWorkItemName, ErpWorkItemId, ErpDocumentId, ErpContentType, ErpDocumentUrl
// (each document); General.ErpMissingUrlCount (work item).
(function () {
var names = {};
try {
names = JSON.parse(GlobalVariables.Get("ContentTypeNames"));
} catch (e) {
names = {}; // fall back to the id below
}
var missingUrl = 0;
for (var i = 0; i < batch.Documents.length; i++) {
var doc = batch.Documents[i];
var general = doc.Metadata["General"] || {};
var contentTypeId = String(general["ContentTypeId"] || "").toLowerCase();
var url = general["DocURL"] || "";
if (!url) { missingUrl++; }
builder.SetDocumentMetadata(doc.DocumentId, "General", "ErpWorkItemName", batch.BatchName || "");
builder.SetDocumentMetadata(doc.DocumentId, "General", "ErpWorkItemId", batch.BatchId);
builder.SetDocumentMetadata(doc.DocumentId, "General", "ErpDocumentId", doc.DocumentId);
builder.SetDocumentMetadata(doc.DocumentId, "General", "ErpContentType", names[contentTypeId] || contentTypeId);
builder.SetDocumentMetadata(doc.DocumentId, "General", "ErpDocumentUrl", url);
}
builder.SetBatchMetadata("General", "ErpMissingUrlCount", String(missingUrl));
})();
How it works¶
- Every value the ERP row needs is copied into one
General.Erp...key per document, so the Insert Into Data Source mapping is one key per column. - The content type id is lowercased before the lookup, so the variable's keys must be lowercase too. An unknown id falls back to the id itself, so the column is never empty.
- A missing or broken
ContentTypeNamesdoes not stop the work item: thetry/catchfalls back to ids. General.ErpMissingUrlCountcounts documents without a link, so a route can stop them before the insert.
Test it¶
- Run one work item with two documents through upload on a non-production tenant.
- In Monitor or Insight, check the five
General.Erp...keys on each document. - Check the two new rows in the ERP table.
You should see
two ERP rows with the work item name, both ids, the content type's name (for example Vendor Invoice) and a working document link, and General.ErpMissingUrlCount = 0 on the work item.
What can go wrong¶
Watch out
ErpDocumentUrlis empty. The Custom Step runs before Upload To Destination, or the destination returns no link. Move the step after the upload, and checkGeneral.DocURLon an uploaded document.ErpContentTypeshows a GUID. The id is not inContentTypeNames, or the variable is missing. Add the id, in lowercase.- The insert writes the same value on every row. The mapping reads the key at work-item level. Set it to document level.
- Upload To Destination fails with "has no assigned content type". The document has no content type. Classify the document, or set it with
builder.SetContentTypebefore the upload. - Duplicate rows after a retry. Turn on Prevent Duplicates in Insert Into Data Source, and include
ErpDocumentIdin the row so duplicates can be detected.