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

HI Pradeep,


Going through with your soultion for this particular problem.


I have a simillar but little different query.


I have a module where requests are handled by certain levels of group. for rg: if i raise a   hardware request there are 4 levels who will handled that request. the request will get assigned automatically to level 1 group. Simiilary if i raise a software request there are 4 levels with different levels groups but few common groups in both hardware and software. now i am in a situation where i want to make first assignment group or level 1 readonly so that only level 2 have the authority to assign or re-assign the ticket. what i have done now. here is my solution


1) Create a new field assignment counter of integer type.


2) Write a before BR to increment the assignment counter whenever group changes


if (current.assignment_group.changes()) {


current.u_assignment_counter += 1; }


3) Write an onload client script with the condition u_assignment_counter == 1


        Pass the current logged in user & assignment group parameter to script include


4) on the script include query the sys_user_grmember table with the logged in user and assigment group parameter


return true


5) on the response(client call back function) set the assignment group field to read only.


but due to some reason it not working. can add or suggest something else to make it working?


HI Pradeep,


Going through with your soultion for this particular problem.


I have a simillar but little different query.


I have a module where requests are handled by certain levels of group. for rg: if i raise a   hardware request there are 4 levels who will handled that request. the request will get assigned automatically to level 1 group. Simiilary if i raise a software request there are 4 levels with different levels groups but few common groups in both hardware and software. now i am in a situation where i want to make first assignment group or level 1 readonly so that only level 2 have the authority to assign or re-assign the ticket. what i have done now. here is my solution


1) Create a new field assignment counter of integer type.


2) Write a before BR to increment the assignment counter whenever group changes


if (current.assignment_group.changes()) {


current.u_assignment_counter += 1; }


3) Write an onload client script with the condition u_assignment_counter == 1


        Pass the current logged in user & assignment group parameter to script include


4) on the script include query the sys_user_grmember table with the logged in user and assigment group parameter


return true


5) on the response(client call back function) set the assignment group field to read only.


How can we achieve the same for Service Catalog? On a Portal View.

I have a variable (from variable set) on a service catalog that should be visible only to a group's members.

Hi there, would you please explain what is   var gate = g_form.getValue('u_field'); used for?

Hi Paula,

It is my understanding that it is used to take the value of custom field called 'u_field' (insert any field) and put it into the variable called 'gate'. Then, later on you can use 'gate' instead of having to use 'g_form.getValue...' every time, farther down. Makes coding easier.