Use an API Result in a Custom Step¶
A procurement team keeps its vendor master in a vendor service. Each invoice work item carries a vendor code. Before approval, every document needs the vendor's name, status and payment terms from that service. A Custom Step cannot call the network, so the pattern has two steps: Call External API makes the request and stores the response text in the work item's ApiResults bucket, then a Custom Step parses it and writes the fields.
At a glance
- Extension point Call External API, then a Custom Step
- You need work-item field
Index.VendorCode; a vendor service that returns JSON like{"code":"V100","name":"...","status":"Active","termsDays":30}; permission to edit the workflow. - Teaches A URL built with Liquid,
Error BehaviorContinue, and safeJSON.parseofApiResults.
Set it up¶
- In the Workflow designer, add a Call External API step named "Get Vendor":
| Setting | Value |
|---|---|
| External API URL | Gear icon, then Liquid Template: https://api.example.com/vendors/{{ WorkItem.Metadata.Index.VendorCode }} |
| REST Verb | GET |
| HTTP Headers | Your service's auth header |
| Response Metadata Key | VendorRecord |
| Timeout | 30 |
| Error Behavior | Continue (so a 404 for an unknown vendor reaches the Custom Step) |
- After it, add a Custom Step named "Apply Vendor". Paste the script below.
- From "Apply Vendor", draw a route to a review step with the Javascript condition
(workItem.Metadata["General"] || {})["VendorLookup"] !== "OK". Make the normal path the default route. - Save the workflow.
Script¶
After "Get Vendor", the work item holds ApiResults.LastHttpStatusCode and ApiResults.VendorRecord (the body, as text). The script writes document Index.VendorName, Index.VendorStatus, Index.PaymentTerms, and work item General.VendorLookup.
// Custom Step "Apply Vendor". After the Call External API step "Get Vendor".
// Reads: ApiResults.LastHttpStatusCode, ApiResults.VendorRecord (work item).
// Writes: Index.VendorName, Index.VendorStatus, Index.PaymentTerms (each document);
// General.VendorLookup (work item): "OK" or the reason it failed.
(function () {
var api = batch.Metadata["ApiResults"] || {};
var status = api["LastHttpStatusCode"] || "";
var vendor = null;
var outcome = "";
if (status === "404") {
outcome = "Vendor not found";
} else if (status !== "200") {
outcome = "Vendor service returned HTTP " + (status || "none");
} else {
try {
vendor = JSON.parse(api["VendorRecord"] || "");
outcome = "OK";
} catch (e) {
outcome = "Vendor service response was not JSON";
}
}
builder.SetBatchMetadata("General", "VendorLookup", outcome);
if (!vendor) {
return; // allowed here: we are inside a function
}
for (var i = 0; i < batch.Documents.length; i++) {
var id = batch.Documents[i].DocumentId;
builder.SetDocumentMetadata(id, "Index", "VendorName", String(vendor.name || ""));
builder.SetDocumentMetadata(id, "Index", "VendorStatus", String(vendor.status || ""));
builder.SetDocumentMetadata(id, "Index", "PaymentTerms", vendor.termsDays ? "Net " + vendor.termsDays : "");
}
})();
How it works¶
- The Liquid Template puts the vendor code into the URL at run time. The work item's
Index.VendorCodemust be set before the call. - With Error Behavior Continue, a 404 or a 500 still reaches the Custom Step instead of stopping the work item.
- The Custom Step checks the status first. Then it parses the body inside
try/catch, so a non-JSON answer becomes a reason, not a script error. General.VendorLookupisOKor the reason, in words. The route sends anything butOKto review.- Every value from the response is wrapped in
String(...), because metadata values must be strings.
Test it¶
- Run one work item with a known vendor code and one with an unknown code.
- Open each in Monitor or Insight. Check
ApiResults.VendorRecordandGeneral.VendorLookupon the work item, andIndex.VendorNameon the documents.
You should see
General.VendorLookup = OK and the vendor fields filled on the first work item, and General.VendorLookup = Vendor not found with the work item in the review step on the second.
What can go wrong¶
Watch out
- The Custom Step never runs after a 404. The call step's Error Behavior is Stop. Set it to Continue.
ApiResults.VendorRecordis missing. The Response Metadata Key is empty. The body is stored only when the key is set.- The status is from an older call. A timeout writes no status. If the work item can pass the call twice, clear the key in a Custom Step before the call:
builder.SetBatchMetadata("ApiResults", "LastHttpStatusCode", ""). - The URL ends in
/vendors/.Index.VendorCodewas empty, so Liquid rendered nothing. Liquid does not report a missing key. ReferenceError: http is not defined. Older examples calledhttp.Getinside a Custom Step. That object does not exist. Use the two-step pattern on this page.