Stamp the Approving User¶
An accounts payable reviewer ticks Approved on an invoice. The invoice should record who approved it without the reviewer typing their name, and the name must not change when someone else opens the invoice later.
At a glance
- Extension point Custom Index Behavior
- You need
Approved(Boolean) andApprovedBy(Textbox) on the content type; permission to edit content types in the Connections app - Teaches
context.username, writing a value only once, skipping the server run withcontext.app
Set it up¶
- In the Connections app, open the invoice content type.
- Select the
Approvedfield. Open Behaviors > Add a Behavior > Custom Behavior. - Name it
Stamp approver, paste the script below, and select Save & Close. - Optional: make
ApprovedByread-only on the content type so users cannot type in it.
Script¶
Custom Behavior on Approved
// Attach to: Approved
// Reads: Approved, ApprovedBy, context. Writes: ApprovedBy.
var approved = (panel["Approved"].RuntimeValue || "").toString().toLowerCase() === "true";
var approvedBy = (panel["ApprovedBy"].RuntimeValue || "").toString();
if (context.app !== "worker") {
if (approved && approvedBy === "" && context.username) {
// Stamp only when nobody is stamped yet, so a later viewer does not replace the approver.
builder.SetFieldRuntimeValue("ApprovedBy", context.username, true);
}
if (!approved && approvedBy !== "") {
builder.SetFieldRuntimeValue("ApprovedBy", "", true);
}
}
How it works¶
RuntimeValueof a Boolean field is the string"true"or"false", so the script compares in lowercase.- The script runs on every panel load, not only when the box is ticked. The
approvedBy === ""check keeps the first approver. - On the server
context.usernameis""andcontext.appis"worker". The outerifskips the server runs (Index Your Documents, Run Indexing Behaviors, Start QC Task, Search bulk edit), so they never erase the name. - Clearing
Approvedclears the name, so a withdrawn approval leaves no approver.
Test it¶
- In the editor, select Test. Tick
Approvedin the preview panel.ApprovedByshows your user name. - Clear
Approved.ApprovedByis cleared. - Save. Approve an invoice in the Index app as one user, then open it as a second user.
You are done when the second user sees the first user's name in ApprovedBy, and the name is still there after the work item passes a Run Indexing Behaviors or Start QC Task step.
What can go wrong¶
Watch out
ApprovedBystays empty and the field shows "could not be fired". The script usesContext.Username. The object iscontextand the property isusername, all lowercase.- The approver changes when someone else opens the invoice. The
approvedBy === ""check was removed. - The name disappears after a workflow step. The
context.app !== "worker"check was removed. Start QC Task runs behaviors by default, so this matters on every QC workflow. - The user name is in an unexpected form.
context.usernameis the identity name the platform uses. Check it once in the preview panel before you build reports on it. - This is not an audit trail. The value is set in the user's browser. Use the work item history for a record of who approved.
Related¶
- Show and Hide Fields Conditionally: show
ApprovedandApprovedByonly when review is needed. - Hide a Field in One App: more on
context.app. - Custom Index Behavior Reference