Show Work Item Data with gizmo.API¶
A claims reviewer needs to see work item values that are not on the task's Index Panel, for example the source system and the received date that an earlier step stored, plus the documents in the work item. This gizmo learns the work item id from a task input, reads the work item through gizmo.API as the signed-in user, and shows the chosen fields and documents.
At a glance
- Extension point Custom Gizmo calling the KnowledgeLake API
- You need A Start A Task step; the feature
openapi_api_ft_manage_workitemsand Use Custom Gizmos for every task user; permissions Create Custom Gizmos and Manage Layouts - Teaches Passing the work item id in,
DeclarePermissions,gizmo.API.V1_KWorkItemAPI.GetWorkItem, and reading metadata buckets
A work item was called a batch in older versions, and some names still say batch, for example the "Batch ID" field below.
| Key | Kind and meaning |
|---|---|
work-item-id |
Input, string. The work item id, mapped from Batch ID. |
fields-to-show |
Input, string. Bucket/Key pairs, comma-separated. |
work-item-name |
Result, string. The work item name, for the record. |
How a gizmo learns its work item id¶
A gizmo has no built-in work item id. Pass it in as a task input:
graph TD
A["Work item: General, Batch ID"] --> B["Start A Task input mapping"]
B --> C["Layout input work-item-id"]
C --> D["gizmo.Config"]
D --> E["gizmo.API.V1_KWorkItemAPI.GetWorkItem"]
- The gizmo declares an input, here
work-item-id. - In the layout, the input is aliased with Use Value from Input.
- The Start A Task step maps that input to the work item id: Level Batch (work item), Bucket General, field Batch ID.
Set it up¶
- Go to Configuration › Services › Custom Gizmos › New Custom Gizmo, title "Work Item Details". Paste the files below and save. Open
gizmo.jsonand check thatpermissionslistsopenapi_api_ft_manage_workitems. - In the Tasks app, go to Layouts › Edit Layout › Add Gizmo › Custom Gizmo › "Work Item Details". Required Permissions shows the work item permission.
- In Configure Gizmo:
- "Work item id": tick Use Value from Input, Input Name
work-item-id. - "Fields to show": enter
Index/Source System, Index/Received Date. - In the Start A Task step, Input Mappings:
work-item-id› Level Batch, Bucket General, field Batch ID. - Give every task user the feature
openapi_api_ft_manage_workitemsand Use Custom Gizmos. Without them the task page does not load.
The files¶
<script src="{project}/main.js"></script>
<style>
body { font-family: sans-serif; padding: 12px; }
h3 { margin: 0 0 8px 0; }
dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 0; }
dt { font-weight: bold; }
ul { padding-left: 18px; }
#error { color: #c62828; }
</style>
<h3 id="title">Loading work item...</h3>
<dl id="fields"></dl>
<ul id="documents"></ul>
<div id="error"></div>
// Work Item Details gizmo.
// Inputs: work-item-id (string, mapped from General / Batch ID),
// fields-to-show (string, "Bucket/Key" pairs, comma-separated).
// Results: work-item-name.
// API permissions: ManageWorkitems. Outside origins: none.
gizmo.SetInputSchema(function (schema) {
schema.AddString("work-item-id")
.SetDisplayName("Work item id")
.SetDescription("Map this input to Batch ID in the Start A Task step")
.ConfigureUI(function (ui) { ui.AsTextbox(); });
schema.AddString("fields-to-show")
.SetDisplayName("Fields to show")
.SetDescription("Bucket/Key pairs, comma-separated")
.SetDefaultValue("Index/Source System, Index/Received Date")
.ConfigureUI(function (ui) { ui.AsTextbox(); });
});
gizmo.DeclarePermissions(function (builder) {
builder.AddPermission(APIPermissions.ManageWorkitems);
});
gizmo.OnReceiveConfig(function (config) {
});
gizmo.OnShow(function () {
var workItemId = (gizmo.Config.GetString("work-item-id", "") || "").trim();
if (!workItemId) {
showError("No work item id. Map work-item-id to Batch ID in the Start A Task step.");
return;
}
gizmo.API.V1_KWorkItemAPI.GetWorkItem({ WorkItemId: workItemId })
.then(function (response) {
if (!response || !response.Success) {
throw new Error("GetWorkItem returned " + (response ? response.MessageKey : "no response"));
}
render(response.Data);
})
.catch(function (err) {
console.error("Could not read the work item", err);
showError("Could not read the work item. Check your permissions and the work item id.");
});
});
function render(workItem) {
var documents = workItem.Documents || [];
document.getElementById("title").textContent =
(workItem.Name || "Work item") + " (" + documents.length + " documents)";
var list = document.getElementById("fields");
list.textContent = "";
gizmo.Config.GetString("fields-to-show", "").split(",").forEach(function (pair) {
var parts = pair.split("/");
if (parts.length !== 2) { return; }
var bucket = parts[0].trim();
var key = parts[1].trim();
var values = (workItem.Metadata || {})[bucket] || {};
var value = Object.prototype.hasOwnProperty.call(values, key) ? values[key] : "(not set)";
addRow(list, key, value);
});
var docList = document.getElementById("documents");
docList.textContent = "";
documents.slice().sort(function (a, b) { return a.Index - b.Index; }).forEach(function (doc, index) {
var item = document.createElement("li");
var fileName = (doc.Files && doc.Files.length > 0) ? doc.Files[0].FileName : "(no file)";
item.textContent = "Document " + (index + 1) + ": " + fileName + ", " + (doc.Pages || []).length + " page(s)";
docList.appendChild(item);
});
gizmo.Result.SetString("work-item-name", workItem.Name || "");
gizmo.PublishResult();
}
function addRow(list, label, value) {
var dt = document.createElement("dt");
dt.textContent = label;
var dd = document.createElement("dd");
dd.textContent = value;
list.appendChild(dt);
list.appendChild(dd);
}
function showError(text) {
document.getElementById("title").textContent = "Work item details";
document.getElementById("error").textContent = text;
}
How it works¶
DeclarePermissionsputsopenapi_api_ft_manage_workitemsingizmo.json. The task page refuses to load for a user who lacks it, so the gizmo never fails halfway.GetWorkItemruns through the task page as the signed-in user. The gizmo never sees a token.- The call can fail two ways: the Promise rejects, or it resolves with
Success: false. The code handles both. - Metadata is a dictionary of buckets (
Index,Generaland others), each a dictionary of string values. Bucket and key names are case-sensitive. textContent(notinnerHTML) keeps metadata values from being read as HTML.
Test it¶
- Run a work item with two or more documents through the workflow to the Start A Task step. (A test task from the layout designer has no real work item unless you paste a work item id into
work-item-id.) - Open the task with the browser console open.
You should see
the work item name and document count in the title, the chosen fields with their values, one line per document, and "API Call to /api/V1_KWorkItemAPI/GetWorkItem" in the console.
What can go wrong¶
Watch out
- The task page does not load: You are missing the "..." permission required by the Work Item Details Gizmo. The user lacks
openapi_api_ft_manage_workitems. Grant it, or remove the gizmo from that user's layout. - "No work item id." The Input Mapping is missing, or it maps to the wrong option. Use Level Batch, Bucket General, field Batch ID.
- A field shows "(not set)". Bucket and key are case-sensitive and must match the metadata exactly, spaces included.
- The values are old.
GetWorkItemreturns the work item as stored. Index edits the user has not saved in the task are not in it.
Variations¶
- Pick the primary document. Draw a radio button per document instead of a list item. Call
gizmo.BlockTask()inOnShow(unlessgizmo.IsViewOnlyMode()), and on a pick setgizmo.Result.SetString("primary-document-id", doc.Id), publish, and callgizmo.UnblockTask(). Put the gizmo on the last page, because the choice is cleared when the gizmo reloads. - Show possible duplicates. Call
gizmo.API.V1_ProcessingAPI.QueryForWorkItems({ WorkitemName: workItem.Name })and declareAPIPermissions.BatchRead. See API Reference.
Related¶
- API Reference:
gizmo.APIand permissions - KWorkItem API Reference
- Debugging Custom Gizmos