Collapse the form section

snowlearner
Kilo Expert

How can we collapse the form section on load.

13 REPLIES 13

coryseering
ServiceNow Employee
ServiceNow Employee

Hi snowlearner,



If you are not using tabs, each form section except the default one has an arrow to collapse that section. Any tab section with an arrow is collapsable, via the "hideReveal" function on the page. It takes the sys_id of the tab section as an argument.



In the simplest case, you could create an onLoad Client Script for the form in question, which calls the function to hide the section. Something like:



function onLoad() {


        hideReveal('991f88d20a00064127420bc37824d385');


}



Where you pass in the sys_id of the actual section you want to hide.



You may want to get a little more robust in your solution, though. You can use the g_tabs2Sections object to get a list of all the sections in your form (it's the tabs property on that object). This is an array of the actual form elements that encompass those tabs. You can iterate through those and pull out the attributes "data-section-id" and tab_caption to get the sys_id and the human-readable name of the section respectively.



You could write a function, for instance, which takes the human-readable name "Notes" and returns "991f88d20a00064127420bc37824d385" (the sys_id of that form section in this example), and then calls the hideReveal function with "991f88d20a00064127420bc37824d385" as it's argument.



This relies on the existing DOM structure of the form, and may stop working with future upgrades to the UI. I recommend opening an Enhancement request in Hi to improve the API around sections, to make this easier and remove reliance on reading attributes off DOM elements.


Thanks Cory for sharing the information. But I want to collapse the sections by default whenever the form loads. As the current behaviour is whenever the form loads, form section expands.



I am looking to script which can collapse the form section not hide the section.


It sets a user preference:



Capture.PNG



My thought is either you could have a script that sets the user preference with setPreference('user_preference_name', 'user_preference_value');   or you could set it once and make it system.


Hi snowlearner,



hideReveal collapses sections. An onLoad script which iterates over the section ID's and calls hideReveal(id) as I described is exactly what you are looking for.