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

@ssoniy 

it means you are not picking the correct form section name

to know the form section name inspect the element and check the name and use this

g_form.setSectionDisplay('related-records', false);

find_real_file.png

Regards
Ankur

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

@Ankur Bawiskar how can I hide these sections through ACL? since I'm using client script the value can be visible through DOM Chrome. The value is very confidential so can you advise on this, please?

@ssoniy 

Can you Please check are you mentioning the right Section in script and also check is thr any other script making to disply it forcefully.

 

Regards,

Abdul

Usually when there is any mandatory field in any section it does not let the section hide. So make sure to remove the mandate for each field before hiding the section.

e.g

g_form.setMandatory('field1',false);

g_form.setMandatory('field2',false);

g_form.setSectionDisplay('<section_name>',false)

 

To know the section names use g_form.getSectionNames() on broswer console. Below are the section names from the incident table.

find_real_file.png

 

Please mark correct/helpful if it helps.

Thanks

Deepak

Anurag Tripathi
Mega Patron
Mega Patron

Hi ,

If you section name has space then it gets a bit tricky, check this out

Show/Hide Form Sections with Multiple Spaces In the Name - Developer Community - Blog - ServiceNow C...

 

-Anurag

-Anurag