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

Hello Karthik,



FYI: This script will not work at the client side and needs to be executed at server side via GlideAjax or set the scratchpad value to true or false at display BR and use it at client side.


Yes Pradeep, I missed that Michael was trying on the client side


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


    }


}



How do I make it so that admins are not impacted (they see all states)?


Modify if condition as if (gate == 'true' && !g_scratchpad.grp && !g_user.hasRole('admin')){