Unable to hide sections using setSectionDisplay()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2021 10:25 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2021 10:46 PM
Hi,
Put an alert and check the value once
alert(g_scratchpad.user);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2021 10:41 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2021 10:50 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2021 10:57 PM
PF the below URL.
https://servicenowguru.com/scripting/client-scripts-scripting/showhide-form-section/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2021 02:52 AM
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