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.

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

eican
Kilo Guru

Did you review this one?


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



Also you could modify your logic to only hide it if the user is not a member of the group.


In all other cases no need to take action as the user is allowed to see it.


Yes, I have reviewed that site, and I actually have tried reversing it to where i you're not a member of the group.   No luck.


Before looking deeper into the functionality of hiding the tab: Did you make sure that your code for the "if"-statement works?


To me it seems like you are mixing server and client side code.



Check this one:


http://www.servicenowguru.com/scripting/user-object-cheat-sheet/



In your script


Client code: "var me = g_user.userID();"


Server code: if (me.isMemberOf('7efcb8956fa21100dd3253eabb3ee40c'){


I find "isMemberOf" only in the context of the user object on the server side.



You need to get the user object itself. Before you start running a server query: Better do this using a asynchronous query so your users don't have a delay when the form loads. (http://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/)


Another, even faster, option is, to run a "display" BR and populate all groups of the current user in the scratchpad.


http://wiki.servicenow.com/index.php?title=Business_Rules#Display_Business_Rules


Ok, I was finally able to get the section to hide, but now it's hiding even for those in the group.


I did a Display BR:


g_scratchpad.groups = (gs.getUser().getMyGroups());



And a Client Script:


function onLoad() {


      if(g_scratchpad.groups != '7efcb8956fa21100dd3253eabb3ee40c'){


              var sections = g_form.getSections();


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


      }


}