Client-side mandatory field enforcement is a UX convenience, not a security control. The setMandatory() method only prevents form submission through the standard UI — it does absolutely nothing against REST API calls, Import Sets, or Business Rules that modify records. Most developers implement the client script and think they're done, then wonder why their validation gets bypassed in production. The real enforcement must happen server-side through Business Rules or ACLs, with the client script providing immediate feedback to prevent round trips.

When to use this pattern

  • Interactive forms where field requirements change based on user selections (category, state, priority)
  • Improving user experience by providing immediate validation feedback without server round trips
  • Complex conditional logic where multiple field values determine mandatory status
  • Wizard-style workflows where requirements change as users progress through stages

When NOT to use this pattern

  • Security-critical validations — use server-side Business Rules with gs.addErrorMessage() to prevent record creation
  • Fields that should always be mandatory — set the Mandatory checkbox on the dictionary entry instead
  • Service Portal — client scripts don't execute in portal widgets, use widget client controllers
  • Mobile applications where client scripts have limited support — implement server-side validation only

Key behaviors and gotchas

  • Always check isLoading and isTemplate — without these guards, the script fires during form load and template application, causing incorrect mandatory states
  • State field values are integers, not strings — state == '1' works but state == 1 is more precise and won't break if the field type changes
  • Clear field values when removing mandatory status — users expect fields to reset when they become irrelevant, preventing stale data submission
  • Pair setMandatory() with setFieldDisplay() — making hidden fields mandatory confuses users and breaks form submission
  • Reference field mandatory validation only checks the sys_id value — display value changes don't trigger validation until the record resolves
  • Scripts in Global scope apply to all applications — scope your conditions carefully to avoid affecting unrelated tables
⚠️

Client-side mandatory validation is completely bypassed by REST API calls, Import Sets, and server-side record modifications. Always implement matching server-side validation in Business Rules for any field that affects business logic or compliance requirements.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime