Show and Hide Fields Conditionally¶
An accounts payable team flags some invoices for review. Reviewers need the approval fields on those invoices only, and a rejected invoice needs a reason. Fields that do not apply should be hidden, and values that no longer apply must not stay on the document.
At a glance
- Extension point Custom Index Behavior
- You need A content type with
NeedsReview(Boolean),Approved(Boolean),ApprovedBy(Textbox) andRejectionReason(MultiLineTextbox or Textbox); permission to edit content types in the Connections app - Teaches Visibility that depends on two fields, one script on two trigger fields, clearing stale values with behaviors suppressed
Set it up¶
- In the Connections app, open the invoice content type.
- Select the
NeedsReviewfield. Open Behaviors > Add a Behavior > Custom Behavior. - Name it
Approval visibility, paste the script below, and select Save & Close. - Select the
Approvedfield and add the same script as a second Custom Behavior. The result depends on both fields, and a behavior runs only when its own field changes. - If
RejectionReasonhas a Required behavior, remove it. A Required behavior still applies to a hidden field. See "Variations" for a script rule instead.
Script¶
// Attach to: NeedsReview, and the same script to Approved
// Reads: NeedsReview, Approved.
// Writes: visibility of Approved, ApprovedBy, RejectionReason; clears values that no longer apply.
function isYes(fieldName) {
var v = (panel[fieldName].RuntimeValue || "").toString().trim().toLowerCase();
return v === "true" || v === "yes";
}
var needsReview = isYes("NeedsReview");
var approved = isYes("Approved");
var showReason = needsReview && !approved;
builder.SetFieldIsVisible("Approved", needsReview);
builder.SetFieldIsVisible("ApprovedBy", needsReview);
builder.SetFieldIsVisible("RejectionReason", showReason);
// Clear a field only when it holds something. true = do not run that field's behaviors.
function clearIfSet(fieldName, emptyValue) {
var v = (panel[fieldName].RuntimeValue || "").toString();
if (v !== "" && v !== emptyValue) {
builder.SetFieldRuntimeValue(fieldName, emptyValue, true);
}
}
if (!needsReview) {
clearIfSet("Approved", "false");
clearIfSet("ApprovedBy", "");
}
if (!showReason) {
clearIfSet("RejectionReason", "");
}
How it works¶
isYesreads a field as a string and accepts"true"(a Boolean field) or"yes"(a Textbox a workflow fills).- Visibility is computed from both fields every run, so the result is the same whichever field changed, and on panel load.
ApprovedandApprovedByshow only when review is needed.RejectionReasonshows only when review is needed and the invoice is not approved.- Hidden fields are cleared, so a stale approval or reason never stays on the document.
clearIfSetwrites only when the field holds a value, so a panel load on a clean document changes nothing. The third argumenttruestops the cleared fields' own behaviors from running.
Test it¶
- In the editor, select Test. In the preview panel, tick
NeedsReview.Approved,ApprovedByandRejectionReasonappear. - Tick
Approved.RejectionReasondisappears and is cleared. - Clear
NeedsReview. All three fields disappear, andApprovedandApprovedByare cleared. - Save, then open an invoice in the Index app and repeat.
You are done when the fields follow NeedsReview and Approved in the Index app, and a document saved with NeedsReview cleared has no approval values.
What can go wrong¶
Watch out
- A field never hides. The script compares to the boolean
true.RuntimeValueis a string. UseisYes. - Visibility updates only for one field. The script is attached to one trigger field only. Attach it to both.
- "could not be fired" on the field. A field name is misspelled. Names are case-sensitive.
- The user cannot submit. A Required behavior on a hidden field still blocks. Remove it, or use the variation below.
- Server runs clear approvals. Run Indexing Behaviors and Start QC Task run this script too and write the cleared values. That is correct here: an invoice that does not need review must not carry an approval. If you want different server behavior, wrap the clearing in
if (context.app !== "worker"). - Hiding is not a control. Users can still set the fields through other apps or the API. Enforce approval rules in the workflow.
Variations¶
Required only when visible. Add this to the end of the script so RejectionReason blocks submit only when it shows and is empty:
var reason = (panel["RejectionReason"].RuntimeValue || "").toString().trim();
var reasonOk = !showReason || reason !== "";
builder.SetFieldIsValid("RejectionReason", reasonOk);
if (!reasonOk) { builder.SetFieldValidationMessage("RejectionReason", "Enter a reason for the rejection."); }
Attach the script to RejectionReason too, so the message clears as soon as the user types.
Keep the reason for audit. Remove the if (!showReason) block. The value then stays on the document while the field is hidden.
Related¶
- Stamp the Approving User: fill
ApprovedBywhenApprovedis ticked. - Hide a Field in One App
- Validate Fields with Your Own Messages
- Custom Index Behavior Reference