isMemberOf function in client script

Michael Bachme1
Kilo Guru

I'm trying to get certain options in the State field to hide if the current user is not a member of a group. The script I have so far is as follows:

function onLoad() {

  var usr = g_user.getUserID();

  var gate = g_form.getValue('u_field');

  if (gate == 'true' && !usr.isMemberOf('GROUP')){

      g_form.removeOption('state', '3'); //closed complete

      g_form.removeOption('state', '4'); //closed incomplete

      g_form.removeOption('state', '7'); //closed skipped

    }

}

How do I get the opposite of 'isMemberOf'? I was thought it was an exclamation point (i.e. !usr.isMemberOf('group').

Thoughts?


Thanks in advance.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Step 1: Create a display Business rule with script as. Place the below script between function templates in script body.


g_scratchpad.grp = gs.getUser().isMemberOf('PASS GROUP NAME HERE');  



Now update the client script as


function onLoad() {


  var usr = g_user.getUserID();


  var gate = g_form.getValue('u_field');


  if (gate == 'true' && !g_scratchpad.grp){


      g_form.removeOption('state', '3'); //closed complete


      g_form.removeOption('state', '4'); //closed incomplete


      g_form.removeOption('state', '7'); //closed skipped


    }


}



View solution in original post

15 REPLIES 15

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Michael,



gs.getUser().isMemberOf('GROUP') method works at server side. You need to execute this via GlideAjax.


http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0


This will be a challenge for me as I have no experience in GlideAjax. Hints? Tips?


Working on it. Please stand by.


karthik73
Mega Guru

you have to use the getuser() (fetch user reference) function for checking group membership



!gs.getUser().isMemberOf( ­'GROUP'))