FUJI UPDATE: In the Fuji release, ServiceNow finally fixes this with a standard g_form call! You can now hide form sections using their name like this…
g_form.setSectionDisplay('schedule', false);
The first parameter is simply the name of your section, which should be completely lower-case. Sections including spaces should use an underscore in place of the first space with the rest of the spaces removed. For example, a form section with a label of ‘Schedule and Planning’ would require a parameter of ‘schedule_andplanning’. The second parameter is a true/false boolean that indicates whether the form section should be visible or not.
Form sections are a great way to segment information on a form into pieces that make sense to be grouped together. The most common example of the use of form sections is the ‘Comprehensive Change’ form. If you want to learn more about how forms, form sections, and views work I HIGHLY recommend a When customers use form sections, the need often arises to be able to show and hide that form section with a client script onLoad or on some changed field trigger.
People have known how to hide these form sections based on an ID number (which corresponded to the order of the form sections on the form). This method works fine, but it fails as soon as you add a new form section or change the ordering of your form sections in some way. Because of this, there has long been a need to hide form sections based on the section name or caption. Nobody has been able to figure out a way to do this…until now. Read on for the details…
First, for reference purposes, I’ll show you the standard, number-based script for hiding form sections.
Hide a form section…If I wanted to hide the second form section on my form I could use this script…
var sections = g_form.getSections();sections[1].style.display = 'none';
Show a form section…
If I wanted to show the second form section on my form I could use this script…
var sections = g_form.getSections();sections[1].style.display = 'block';
FUJI UPDATE: In the Fuji release, ServiceNow finally fixes this with a standard g_form call! You can now hide form sections using their name like this…
g_form.setSectionDisplay('schedule', false);
The first parameter is simply the name of your section, which should be completely lower-case. Sections including spaces should use an underscore in place of the first space with the rest of the spaces removed. For example, a form section with a label of ‘Schedule and Planning’ would require a parameter of ‘schedule_and_planning’. The second parameter is a true/false boolean that indicates whether the form section should be visible or not.
The method for hiding a form section by name is really just a slight change to the way that the form section is selected. Whereas the number-based method returns all form sections in an array, the name-based method selects just that specific form section based on the form section caption. This method utilizes Prototype selectors to target the specific span that the form section resides in based on the ‘tab_caption’ attribute, but the result is the same.
Hide a form section by name…
If I wanted to hide the form section with a name of ‘Schedule’ I could use this script. Just replace ‘Schedule’ with the name of your form section to use it in your environment.
//Hide the sectionvar section = $$('span[tab_caption_raw="Schedule"]')[0].select('span[id*=section.]')[0];section.hide();//Hide the tab$$('.tab_caption_text').each(function(caption) { if(caption.innerHTML == 'Schedule'){ caption.up('.tab_header').hide(); }});
Show a form section by name…
If I wanted to show the form section with a name of ‘Schedule’ I could use this script. Just replace ‘Schedule’ with the name of your form section to use it in your environment.
//Show the sectionvar section = $$('span[tab_caption_raw="Schedule"]')[0].select('span[id*=section.]')[0];section.show();//Show the tab$$('.tab_caption_text').each(function(caption) { if(caption.innerHTML == 'Schedule'){ caption.up('.tab_header').show(); }});
Automatically selecting the shown form section
In some cases you might want to automatically select the form section tab as it is shown. You can also do this through client scripting.
Want to learn more about controlling the behavior of tabs and form sections in ServiceNow?