What It Does
The setVisible() method controls the visual display of form fields by manipulating the CSS display properties of the field container. When you hide a field, ServiceNow adds CSS classes that set display: none on the field's row element. The field completely disappears from the form layout, and other fields shift to fill the space.
Internally, ServiceNow maintains a visibility state map for each field on the form. When setVisible() executes, it updates this map and immediately applies the corresponding CSS changes. The DOM manipulation happens synchronously, so the visual change is instant. The method also triggers internal form state recalculation to ensure other UI elements respond correctly to the layout change.
The method returns void and provides no feedback about success or failure. If you pass an invalid field name, the method silently does nothing rather than throwing an error. This fail-silent behavior means you won't get console warnings for typos in field names, making debugging more challenging.
Hidden fields maintain their values and validation rules. Mandatory fields that are hidden will still trigger validation errors on form submission unless you also use setMandatory() to remove the requirement. The field data is still included in form submissions and will be processed by server-side Business Rules and other platform mechanisms.
This method works alongside setDisplay() and setReadOnly() to control field presentation. Unlike setDisplay() which can make fields read-only, setVisible() only controls visibility. When you show a previously hidden field, it retains its original editable state unless modified by other methods.
When to Use This
Use setVisible() when you need dynamic field visibility based on user input, record state, or business logic. Common scenarios include showing additional fields when a specific category is selected, hiding irrelevant fields based on user roles, or revealing progressive disclosure sections. This method is perfect for conditional forms where field relevance changes based on other form values.
Avoid using setVisible() for security purposes since client-side scripts can be bypassed. If you need to prevent data entry based on security requirements, implement server-side ACLs or Data Policies instead. Similarly, don't use this method to hide sensitive information—users can inspect the DOM to reveal hidden fields. For permanent field visibility control, configure UI Policies or modify the form layout directly.
Choose setDisplay() instead when you need to make fields read-only while keeping them visible. Use setReadOnly() when you want to disable editing without changing visibility. For complex visibility logic that should persist across sessions, implement UI Policies with proper conditions rather than client scripts.
Return Value
The method returns void (JavaScript undefined) and provides no indication of success or failure. You cannot chain this method or use its return value for conditional logic. The visibility change either happens immediately if the field exists, or silently fails if the field name is invalid or the field is not on the current form.
Since there's no return value to check, validate field names during development by testing in the browser console. Use g_form.getControl() to verify a field exists before calling setVisible() if you're unsure about field availability. The method will work on any field present in the form's field list, regardless of current visibility state.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- No server-side platform mechanisms fire—this is purely a client-side DOM manipulation that doesn't trigger Business Rules, ACLs, or notifications
- Nothing is written to the database—field visibility changes are session-only and reset when the form reloads
- Performance is immediate and lightweight—only CSS classes are modified, no AJAX calls or server communication
- Form layout automatically reflows when fields are hidden or shown, potentially changing the visual structure of the entire form
- Hidden fields are still included in form submissions and will be processed by all server-side logic including workflows and Business Rules
- Mandatory validation still applies to hidden fields—you must use setMandatory(false) separately to remove required field validation
Client scripts can be bypassed by users with browser dev tools. Never rely solely on setVisible() for security or data integrity.