checking if a reference field is in a group

Rick Forristall
Tera Guru

I have a reference field called hiring manager (u_hiring_manager) that reference the user table.

 

In a UI policy I need to find out if the hiring manager is in a group.

 

I've been at this for several hours - not finding any direct solutions.

 

I've tried:

var hm = g_form.getReference('u_hiring_manager', setAValue);

 

to get the hiring manager's user information. Then I tried:

hm.isMemberOf('group name') - didn't seem to work

hm.sys_id.isMemberOf('group name') - didn't seem to work.

 

Any help will be much appreciated.

 

Thanks,


Rick Forristall

Goodwill of Central Arizona

1 ACCEPTED SOLUTION

I would put an alert in as the first line in your 'onCondition' function to check that the UI policy condition is even firing and getting into the script.   I have mine set up as a client script.   You can log into demo003 here...



https://demo003.service-now.com/side_door.do


View solution in original post

18 REPLIES 18

Mark, here's my complete script. How can I view your demo003 instance?


I'm not getting the alert when I change the hiring manager field to a non Dept Head group person.



function onCondition() {



  // https://community.servicenow.com/message/707606?et=watches.email.thread#707606


  var grp = new GlideRecord('sys_user_grmember');


  grp.addQuery('group.name', 'GICA: Department Heads');


  grp.addQuery('user', g_form.getValue('u_hiring_manager'));


  grp.query(groupMemberCallback);



  function groupMemberCallback(grp){


  //If user is a member of selected group


  if(grp.next()){


  //Do something


  g_form.setValue('u_status','Approved by Dept Head');


  //alert('Is a member');


  }


  else{


  // just for testing


  alert('Is not a member');


  }


  }


}


I would put an alert in as the first line in your 'onCondition' function to check that the UI policy condition is even firing and getting into the script.   I have mine set up as a client script.   You can log into demo003 here...



https://demo003.service-now.com/side_door.do


Mark thanks again,



Hmmm... when I switched to a client script - it worked (I had to create 2: an onLoad and an onChange) because I'm pre-populating the Hiring Manager field based on the current user.



Thanks for your help!



Rick Forristall


Goodwill of Central Arizona


AnishSasidharan
Mega Expert

If you don't like to query the table, you can use the following code



var manager_name = g_form.getReference('u_hiring_manager').user_name;



write one GlideAjax function and pass the manager_name as parameter to the include script function



In Script include under GlideAjax function, you have to write this code



var hm = gs.getUser();


hm =hm. ­getUserByID(parameter value from glideajax);


return hm;



hm will return the true or false value.



Please mark it as answer if it is helpful.



Thanks


Anish


Anish,



Thank you for your reply.



I going to try Mark's suggestion for now. I'm not sure how to write a GlideAjax function and create/use include scripts yet.



Thanks,



Rick Forristall


Goodwill of Central Arizona