Skip to content

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) and RejectionReason (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

  1. In the Connections app, open the invoice content type.
  2. Select the NeedsReview field. Open Behaviors > Add a Behavior > Custom Behavior.
  3. Name it Approval visibility, paste the script below, and select Save & Close.
  4. Select the Approved field 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.
  5. If RejectionReason has a Required behavior, remove it. A Required behavior still applies to a hidden field. See "Variations" for a script rule instead.

Script

Custom Behavior on NeedsReview and Approved
// 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

  • isYes reads 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.
  • Approved and ApprovedBy show only when review is needed. RejectionReason shows 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. clearIfSet writes only when the field holds a value, so a panel load on a clean document changes nothing. The third argument true stops the cleared fields' own behaviors from running.

Test it

  1. In the editor, select Test. In the preview panel, tick NeedsReview. Approved, ApprovedBy and RejectionReason appear.
  2. Tick Approved. RejectionReason disappears and is cleared.
  3. Clear NeedsReview. All three fields disappear, and Approved and ApprovedBy are cleared.
  4. 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. RuntimeValue is a string. Use isYes.
  • 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.