Skip to content

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 Behavior Continue, and safe JSON.parse of ApiResults.

Set it up

  1. 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)
  1. After it, add a Custom Step named "Apply Vendor". Paste the script below.
  2. 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.
  3. 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
// 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.VendorCode must 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.VendorLookup is OK or the reason, in words. The route sends anything but OK to review.
  • Every value from the response is wrapped in String(...), because metadata values must be strings.

Test it

  1. Run one work item with a known vendor code and one with an unknown code.
  2. Open each in Monitor or Insight. Check ApiResults.VendorRecord and General.VendorLookup on the work item, and Index.VendorName on 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.VendorRecord is 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.VendorCode was empty, so Liquid rendered nothing. Liquid does not report a missing key.
  • ReferenceError: http is not defined. Older examples called http.Get inside a Custom Step. That object does not exist. Use the two-step pattern on this page.