Unable to hide sections using setSectionDisplay()

ssoniy
Tera Contributor

Requirement was to hide sections based on role 'xyz' & 'assigned_to' is the current user.

I wrote display business rule to validate above condition and saved in scratchpad as below:

if ((gs.getUserID() == current.assigned_to) || (gs.hasRole('xyz')) ) {
g_scratchpad.user = true;

}
else {
g_scratchpaduser = false;
}

And called in the onload client script as below:

if (g_scratchpad.user == 'false') {
g_form.setMandatory('mandatory', false);   // tried using this to remove all mandatory fields on all sections
g_form.setSectionDisplay('section1', false );
g_form.setSectionDisplay('section2', false);
g_form.setSectionDisplay('section3', false);
g_form.setSectionDisplay('section4', false);
g_form.setSectionDisplay('section5', false);
} else {
g_form.setSectionDisplay('section1', true);
 g_form.setSectionDisplay('section2', true);
g_form.setSectionDisplay('section3', true);
g_form.setSectionDisplay('section4', true);
g_form.setSectionDisplay('section5', true);
 }

setSectionDisplay() not working .. also tried setDisplay() not working. 

Verified section names ex: section_detail all correct. Please correct me anywhere i have done any mistake?

 

17 REPLIES 17

Hi,

Put an alert and check the value once

alert(g_scratchpad.user);

Ankur Bawiskar
Tera Patron
Tera Patron

@ssoniy 

You should make this change

1) g_scratchpad.user -> you forgot the dot for the else part

2) to be on safer side use 'true' and 'false' instead of true and false for comparision

if ((gs.getUserID() == current.assigned_to) || (gs.hasRole('xyz')) ) {
g_scratchpad.user = 'true';

}
else {
g_scratchpad.user = 'false';
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Pranesh072
Mega Sage
Mega Sage

try this 

 

if (g_scratchpad.user == false) {
g_form.setMandatory('mandatory', false);   // tried using this to remove all mandatory fields on all sections
g_form.setSectionDisplay('section1', false );
g_form.setSectionDisplay('section2', false);
g_form.setSectionDisplay('section3', false);
g_form.setSectionDisplay('section4', false);
g_form.setSectionDisplay('section5', false);
} else {
g_form.setSectionDisplay('section1', true);
 g_form.setSectionDisplay('section2', true);
g_form.setSectionDisplay('section3', true);
g_form.setSectionDisplay('section4', true);
g_form.setSectionDisplay('section5', true);
 }

Abdul_Rahiman
Tera Guru

@ssoniy 

 

PF the below URL.

https://servicenowguru.com/scripting/client-scripts-scripting/showhide-form-section/

thank you .. When I tried with the below code, it removes first two sections on the form as expected but when i gave remaining all 5 sections not working.

var sections = g_form.getSections();

sections[1].style.display = 'none';  // working
sections[2].style.display = 'none'; // working

 

When tried hiding all sections, 3rd ,4th, 5th sections not hiding, position looks correct

var sections = g_form.getSections();

sections[1].style.display = 'none';  
sections[2].style.display = 'none'; 

sections[3].style.display = 'none'; //not working

sections[4].style.display = 'none';//not working

sections[5].style.display = 'none';//not working