The onSubmit client script type is ServiceNow's form validation gatekeeper β€” return false and the submission dies immediately, return true or nothing and it proceeds. The critical mistake beginners make is forgetting that JavaScript functions return undefined by default, which ServiceNow treats as true β€” so your validation runs, shows error messages, then lets the form submit anyway. Always explicitly return a boolean, and use g_form.showFieldMsg() for field-specific errors that highlight the problem input, and g_form.addErrorMessage() for business logic violations that span multiple fields.

When to use this pattern

  • When you need immediate validation feedback without a server round-trip β€” checking required fields, format validation, or simple business rules
  • When validation logic depends only on form data visible to the client β€” no database lookups or complex calculations
  • When you want to prevent unnecessary server processing for obviously invalid submissions
  • When the validation error should highlight specific fields to guide user corrections

When NOT to use this pattern

  • Don't use for validation requiring database queries β€” use server-side Business Rules instead, as client scripts can't reliably access server data
  • Don't rely on this for security validation β€” client-side code can be bypassed, so duplicate critical validations in server-side Business Rules
  • Don't use for complex cross-record validation β€” use GlideAjax to call a Script Include that performs server-side validation

Key behaviors and gotchas

  • All active onSubmit client scripts must return true for the form to submit β€” if any script returns false, submission is blocked
  • Use g_form.showFieldMsg() with type 'error' to highlight specific fields red and show messages directly below them
  • Clear previous field messages with g_form.hideFieldMsg() in onChange scripts, otherwise error messages persist after users fix the issue
  • Form-level messages from g_form.addErrorMessage() appear at the top of the form and don't clear automatically β€” use for business logic violations
  • Scripts execute in the order of their Order field value β€” use consistent ordering (100, 200, 300) to control validation sequence
  • UI Actions with Client checked bypass onSubmit validation β€” include validation in the UI Action's client script if needed
⚠️

Never perform expensive operations in onSubmit scripts β€” they block the UI thread. If you need server validation, show a loading message and use GlideAjax with callbacks instead of synchronous checks.

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