The onSubmit client script is your last line of defense before data hits the server β€” but only if you understand that returning false blocks the save completely. Most developers miss that you need to call g_form.clearMessages() first β€” without it, old validation messages persist and confuse users. The pattern works by accumulating all validation failures before returning a single boolean, ensuring users see every problem at once instead of playing whack-a-mole with errors.

When to use this

  • Complex business logic validation that requires multiple field values or calculations
  • Cross-field validation where one field's value determines requirements for another
  • Conditional mandatory fields that standard field dictionary rules can't handle
  • Data quality validation that needs immediate user feedback before server processing

When NOT to use this

  • Simple mandatory field validation β€” use field dictionary mandatory settings instead
  • Validation requiring server-side data lookups β€” use synchronous GlideAjax calls or Business Rules instead
  • Security-critical validation β€” always validate server-side too, as client scripts can be bypassed
  • Mobile app forms β€” onSubmit scripts don't execute in ServiceNow mobile apps

Key behaviors and gotchas

  • Returning false blocks the save completely β€” the form stays in edit mode with your error messages displayed
  • Use g_form.showFieldMsg() not alert() β€” field messages stay visible while users fix issues
  • Scripts execute in UI Action order β€” if you have multiple onSubmit scripts, lower order values run first
  • Any onSubmit script returning false blocks the save β€” remaining scripts in the chain won't execute
  • Field messages persist until clearMessages() or successful form submission β€” always clear at the start
  • Use getValue() for sys_ids and getDisplayValue() for reference field names β€” mixing them creates comparison bugs
  • Checkbox fields return string 'true'/'false' not boolean β€” always use string comparison for checkbox validation
⚠️

onSubmit scripts don't fire for programmatic saves via GlideRecord or when users bypass the form (like list editing). Always implement matching server-side validation in Business Rules for data integrity.

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