Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Onload Client Script

vipultiwar01
Kilo Contributor

function onLoad() {

  var sections = g_form.getSections();

if (category== 'SERVER') {

sections[1].style.display = 'block';

                      sections[2].style.display = 'block';

} else {

sections[3].style.display = 'none';

                      sections[4].style.display = 'none';

}

}

Please help in the correction of this code. I would like to display the section based on"category" in maintain item form but it is not working. Once I removed the "if else" condition then it is working .

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Vipultiwar01,

Set a default value on Field "catalog" and create an onChange() script on field "catalog".

I've used  g_form.setSectionDisplay() to show or hide sections. I'm also comparing with "server" instead of "SERVER" because ServiceNow converts values to lowercase. The label name is "SERVER" but the value is "server".

onChange Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (newValue == '' || oldValue == newValue) {
        return;
    }
    if (newValue == 'server') {
        g_form.setSectionDisplay('section1', true);
        g_form.setSectionDisplay('section2', true);
        g_form.setSectionDisplay('section3', false);
        g_form.setSectionDisplay('section4', false);
    } else {
        g_form.setSectionDisplay('section1', false);
        g_form.setSectionDisplay('section2', false);
        g_form.setSectionDisplay('section3', true);
        g_form.setSectionDisplay('section4', true);
    }
}

Execution results

1. Case when the form is opened. I've set the category to default to "SERVER"

find_real_file.png

2. Case when "OTHER" is selected.

find_real_file.png