Field visibility in ServiceNow forms is controlled by g_form.setVisible(), but most developers miss the critical interaction between visibility, mandatory state, and form submission. Hidden fields that are mandatory will still block form submission, even when invisible to the user. The pattern requires coordinating visibility with mandatory state and clearing values to prevent phantom validation errors. Unlike UI Policies, Client Scripts give you programmatic control over complex conditional logic, but you're responsible for handling the isLoading parameter correctly to avoid flickering fields during form initialization.
When to use this
- Complex conditional logic that UI Policies can't handle (multiple field dependencies, calculations, or API calls)
- When field visibility depends on values from reference fields or related records
- Dynamic visibility that requires calculations (date comparisons, numeric thresholds, string parsing)
- When you need to coordinate visibility with mandatory state changes or value clearing
When NOT to use this
- Simple show/hide based on choice field values β use UI Policy instead for better performance and maintainability
- Role-based field visibility β use UI Policy with role conditions or ACL Field Access rules instead
- Fields that should be permanently hidden for certain groups β use
sys_ui_elementvisibility conditions or dictionary attributes instead - Mobile forms where Client Script performance matters β UI Policies are lighter weight
Key behaviors and gotchas
- Always check
isLoadingparameter in onChange scripts to prevent field visibility flickering during form initialization - Hidden mandatory fields still block form submission β always coordinate
setVisible()withsetMandatory() - Use
clearValue()on hidden fields to prevent data pollution and validation conflicts when fields become visible again - Client Scripts run only in the browser β hidden fields are still submitted to the server and processed by Business Rules
- Field visibility changes don't trigger onChange events β avoid circular dependencies by checking current state before setting
- Reference field display values may not be available immediately β use GlideAjax for dependent data lookups as shown in the example
Never hide a field without clearing its mandatory state. Hidden required fields create phantom validation errors that block form submission with no visible indication to the user why the form won't save.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.