Use the new setSectionDisplay to hide a section in Fuji

Often it can be useful or even necessary to hide certain fields in forms when they are not needed. This way the instance stays more user friendly and easier to handle. For similar reasons, you may need to hide a section in a form. In order to achieve this, we can use the “classical” approach that is based on the section order and used in client side scripts.

To show the section:
var sections = g_form.getSections();
sections[position of the section].style.display = ‘block’;

To hide the section:
var sections = g_form.getSections();
sections[position of the section].style.display = ‘none’;

This solution implies that the section order must be fixed and be the same in all the forms where the client script will be triggered. Therefore, this approach might be hard to use and to maintain, especially if we modify the section position in the form, then the client script should be updated to make sure that it will continue to hide or show the right section, else it may do it for a wrong section.
This is why today, we are glad to share with you another solution. Indeed, ServiceNow released a fix based on the function setSectionDisplay which simply hides or shows sections based on their names. The name of the section must be written in lowercase and the spaces, if exist, must be replaced by ‘_’

To hide/show section:
g_form.setSectionDisplay(sectionName, false/true);
For example to hide the section “Closure notes” you may use the following :
g_form.setSectionDisplay(closure_notes, false);

Thanks to this function you do not need to use the section order anymore and maintaining sections in the form layout becomes easier.

Hamza Berouil
Hamza is a certified ServiceNow System Administrator and certified Implementation Specialist. He is a Technical Consultant at our Paris office since 2014 and has a strong experience in configuration and implementation for various major European companies.
Recent Posts
Comments
  • RV
    Reply

    Hi Hamza,

    You should update the code to include “” around the section name.

    – RV

Leave a Comment

Start typing and press Enter to search