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 Stanger
Giga Sage

There's not a convenience function for determining group membership client-side.   You have to query for it.   In order to do that most efficiently you'll want to use an asynchronous GlideRecord query with a callback function.   Here's something that should work.   Just make sure you're pointing to the correct group name and field name in the first section.



//Check to see if assigned to is a member of selected group


var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('group.name', 'YOURGROUPNAMEHERE');


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


              alert('Is a member');


      }


      else{


              alert('Is not a member');


      }


}


I've updated the SNGuru user object cheat sheet with this function for future reference.   You can find it here...



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


Hi Mark,


Thank you for your quick reply.



This works when the form loads, but when I change the value of the hiring manager, the else { } doesn't fire.



I have this in a UI policy and have [ x ] onLoad checked and left the Conditions blocks empty so the UI fires all the time.



Am I missing something that doesn't allow the else { } part to run?



Thanks,



Rick Forristall


Goodwill of Central Arizona


I'm not sure what the problem might be.   You can post your entire script here and I can take a look.   I've set this up on demo003 on the incident form.   When the Caller field changes it checks to see if the user is a member of the 'Service Desk' group and it works just fine there.   You might review that and compare against your code.