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

I am trying to use this in an email notification script, with the following code, and it is not working. Can someone please review and assist in this?




var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('group.name', 'IT ServiceDesk');


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


grp.query(groupMemberCallback);


   


if (grp.next()) {


  answer = false;


}


Shane, in a mail script you're using back-end scripting so 'g_form' and the callback function wouldn't be applicable.   You'll just want to use a standard GlideRecord query leveraging the 'current' object to get to record values.



var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('group.name', 'IT ServiceDesk');


grp.addQuery('user', current.requested_by);


grp.query();


if (grp.next()) {


  //Do something here if a member of the group...



}


Thanks for the information Mark, that is working.



If I wanted to add a further "OR" condition, of say comparing one variable value to another, would this be sufficient?



var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('group.name', 'IT ServiceDesk');


grp.addQuery('user', current.requested_by);


grp.query();


if (grp.next()) {


That code looks just like what I just posted.   If you want to do an 'or' condition then it depends on where you want to add it.   Is it part of the query or part of the 'if' statement?


My apologies, looks like it didn't copy to my clipboard. The extra condition check would be part of the 'if' statement, as I want it to be checked in addition to the group membership code, to provide the "False" answer to the advanced condition:



var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('group.name', 'IT ServiceDesk');


grp.addQuery('user', current.u_requested_by);


grp.query();


if (grp.next() || current.u_requested_by == current.requested_for) {




  answer = false;


}