How to Show or Hide Form Sections in ServiceNow Using g_form.setSectionDisplay()
- nathanlee142
- Mar 20
- 3 min read
Updated: Mar 29

In ServiceNow, managing form visibility effectively enhances user experience and data management. One powerful client-side scripting method, g_form.setSectionDisplay(), lets you dynamically control form sections based on user actions or specific conditions. However, correctly identifying the form section names to pass into this method can sometimes cause confusion. This article clarifies how to properly define section names, troubleshoot common errors, and use this method effectively to optimize your ServiceNow forms.
Understanding the g_form.setSectionDisplay() Method and Common Issues
The g_form.setSectionDisplay() method is frequently used in client scripts to dynamically show or hide specific sections within a form. The method requires two parameters:
Section Name: The identifier of the form section to control.
Boolean (true/false): To either show (true) or hide (false) the section.
A common stumbling block is correctly specifying the section name. Users often attempt to use the caption directly or modify the name in the sys_ui_section table without achieving the desired result. Directly modifying the sys_ui_section table carries risks of system update conflicts, maintenance difficulties, and performance degradation. Therefore, use standard features such as the form designer or client scripts.
Correctly Defining Form Section Names
The correct way to identify and define section names in ServiceNow is through a specific naming convention. Here’s the verified approach:
Convert the entire section caption to lowercase.
Replace only the first space in the caption with an underscore (_).
Remove all subsequent spaces entirely.
Remove all non-alphanumeric characters such as ampersands (&).
For example, if your section caption is: Hardware Serial Numbers and Asset Tags
The correct section name becomes: hardware_serialnumbersandassettags
Practical Example: Showing and Hiding Sections
Below is a straightforward example demonstrating how to properly hide a specific form section using a client script:
function onLoad() {
// Correctly defined section name based on naming convention
var sectionName = 'hardware_serialnumbersandassettags';
// Hide the specified form section
g_form.setSectionDisplay(sectionName, false);
}
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == 'hardware') {
// When the 'hardware' option is selected
// Correctly defined section name based on naming convention
var sectionName = 'hardware_serialnumbersandassettags';
g_form.setSectionDisplay(sectionName , true);
} else {
g_form.setSectionDisplay(sectionName , false);
}
}
Verifying Section Names with getSectionNames()
To ensure accuracy, you can use the built-in method g_form.getSectionNames() to retrieve the exact names of sections on a form:
function onLoad() {
var sections = g_form.getSectionNames();
alert(sections);
}
This simple script triggers an alert with the exact names of all form sections, confirming the precise identifiers you need.
Alternative Approach: Using UI Policies for Related Lists
It’s important to note that g_form.setSectionDisplay() is effective for standard form sections but doesn’t control visibility of Related List tabs. For Related Lists, use the following steps:
Navigate to UI Policies.
Create a new UI Policy without conditions.
In the related "UI Policy Actions" list, add a new action.
Select the Related List tab you wish to hide or show and set the visibility accordingly.
Conclusion
Using the g_form.setSectionDisplay() method effectively in ServiceNow relies heavily on correctly defining section names. Adhering to the naming convention—lowercased, with the first space replaced by an underscore, subsequent spaces removed, and non-alphanumeric characters eliminated—is critical. By following these clear guidelines and leveraging built-in tools like getSectionNames(), you can effortlessly manage form visibility and significantly enhance the user experience.
Actionable Next Steps
Review your current ServiceNow forms to identify sections requiring dynamic visibility.
Implement and test client scripts using the correct section naming conventions.
Consider using UI Policies for managing Related Lists effectively.