Use a Data Source Lookup Result¶
An accounts receivable team checks each invoice against the customer's credit limit, which lives in the ERP database. A Custom Step cannot query a database: it has no network and no connection objects. The lookup happens somewhere else and leaves values on the work item. The Custom Step then makes the decision on those values. This page shows where the lookup can happen, and the credit check that follows it.
At a glance
- Extension point Custom Step, after a Lookup behavior or your own endpoint
- You need Document fields
Index.CustomerId,Index.InvoiceTotal, and the looked-up fieldsIndex.CustomerNameandIndex.CreditLimit; permission to edit the workflow. - Teaches Splitting lookup from decision, and telling "not found" apart from "not looked up".
Where a lookup can happen¶
| Option | Use it when |
|---|---|
| A Lookup behavior on a content type field, during an indexing task | A person indexes the document. Picking a value fills related fields from the data source. |
| Call External API to your own endpoint that queries the database | No person is involved, or the query is complex. The endpoint writes fields through the KnowledgeLake API, or returns JSON. |
| A JSON global variable | The table is small and changes rarely. See Fill Fields from a Lookup Table. |
To write values back to a database, use the Insert Into Data Source or Update Rows In Data Source steps, not a Custom Step. See Prepare Fields Before Writing to a Data Source.
Set it up¶
- Set up the lookup with one of the options above, so that
Index.CustomerNameandIndex.CreditLimitare filled for a known customer. - In the Workflow designer, add a Custom Step after the indexing task, or after the Call External API step. Name it "Credit Check". Paste the script below.
- From "Credit Check", draw a route to a review task with the Javascript condition
(workItem.Metadata["General"] || {})["CreditCheck"] === "Review". Make the normal path the default route. - Save the workflow.
Script¶
The script writes document General.CreditCheck (Pass or a reason that starts with Review) and work item General.CreditCheck (Pass or Review).
// Custom Step "Credit Check". After the lookup.
// Reads: Index.CustomerId, Index.CustomerName, Index.CreditLimit, Index.InvoiceTotal (each document).
// Writes: General.CreditCheck (each document and work item).
(function () {
function toNumber(text) {
var n = parseFloat(String(text || "").replace(/[^0-9.\-]/g, ""));
return isNaN(n) ? null : n;
}
var anyReview = false;
for (var i = 0; i < batch.Documents.length; i++) {
var doc = batch.Documents[i];
var f = doc.Metadata["Index"] || {};
var result;
if (!f["CustomerId"]) {
result = "Review: no customer id";
} else if (!f["CustomerName"]) {
// The lookup ran but found nothing: the id is not in the data source.
result = "Review: customer " + f["CustomerId"] + " not found";
} else {
var limit = toNumber(f["CreditLimit"]);
var total = toNumber(f["InvoiceTotal"]);
if (limit === null || total === null) {
result = "Review: missing credit limit or total";
} else if (total > limit) {
result = "Review: total " + total.toFixed(2) + " over credit limit " + limit.toFixed(2);
} else {
result = "Pass";
}
}
if (result !== "Pass") { anyReview = true; }
builder.SetDocumentMetadata(doc.DocumentId, "General", "CreditCheck", result);
}
builder.SetBatchMetadata("General", "CreditCheck", anyReview ? "Review" : "Pass");
})();
How it works¶
- The script never queries anything. It reads the values the lookup left, so it works the same whichever option filled them.
- A customer id with no name means the lookup ran and found nothing. The script says so, instead of treating an empty credit limit as zero.
- Each document gets its own reason. The work item gets one value,
PassorReview, for the route. - If your own endpoint does the lookup, it can write
CustomerNameandCreditLimitwithV1_KWorkItemAPI/UpsertDocumentMetadata, and this script works unchanged. Or it returns JSON for the Custom Step to parse. See Use an API Result in a Custom Step.
Test it¶
- Index three documents: a valid customer under the limit, one over the limit, and one with an unknown id.
- Open the work item in Monitor or Insight and check
General.CreditCheckon each document and on the work item.
You should see
Pass on the first document, Review: total ... over credit limit ... on the second, Review: customer ... not found on the third, and General.CreditCheck = Review on the work item, which takes the review route.
What can go wrong¶
Watch out
- Lookup values are empty although the customer exists. Data source lookups can be case-sensitive. Match the case of the stored id, or normalize it in the source.
- Every document says "not found". The lookup did not run before this step, for example because the indexing task was skipped. Put the step after the lookup on every path.
- An older example called
http.Getor a database inside the Custom Step. Neither exists. Move the lookup to one of the options above.