Hiding Form Sections Based on User Group

allison_holt
Giga Expert

I am trying to hide a form section based on a users group, which is not listed on the form itself.

 

I tried the following client script and it's doesn't hide the tab.

 

function on Load(){

var me = g_user.userID();

if (me.isMemberOf('7efcb8956fa21100dd3253eabb3ee40c'){

var sections = g_form.getSections();

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

} else {

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

    }

}

 

Any help as to what I can do to get this to work would be appreciated!

31 REPLIES 31

Abinaya
Mega Expert

Hi Allison Walker,



You can try out this,



Display Business Rule :-


g_scratchpad.groups = gs.getUser().isMemberOf('7efcb8956fa21100dd3253eabb3ee40c'); // try using Name of the group if the sys_id doesn't work (i tried with the Name of the group and it worked)



Client script :-


function onLoad() {


      if(g_scratchpad.groups == false){


              var sections = g_form.getSections();


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


      }


}


I was able to resolve this issue.   Thanks for your comment though!


Hi Allison,


Do you recall what you did to resolve the issue?



I'm trying to do the same thing (sorta).   Would like to hide a form section based on one or multiple groups or maybe use the group parent field as the trigger.


cosminux
Giga Contributor

New g_form method as per the update on the servicenowguru site. Much easier to us.



g_form.setSectionDisplay('your_section_name_here', false/true);


trdaniels
Tera Expert

Here is one example using a client script:



Name: Hide Governance Section


Table: Change Request [change_request]


Type: onLoad



Global [checked]



Script:


function onLoad() {


      try {


              var changeMGR = g_user.hasRole('change_mgr');


              if (!changeMGR) { //if a user does not have the role, remove governance section


                      g_form.setSectionDisplay('governance', false);


              }


      } catch (e) {


              console.log('error= ' + e);


      }


}