Query "sys_user_grmember" table for user in specific group

Arjun Chamaraj
Tera Contributor

I need to access "sys_user_grmember" table for user being part of a specific group, if so return the user and match requested_for user.

I'm having problem when a user is part of multiple groups, then the script only checks for first group and returns false.

need help for a recursive search and return true if he is part of mentioned group.

*********************************************************************************************

answer = ifScript();

function ifScript() {

var usr=current.variables.requested_for;
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', usr);
gr.query();
while(gr.next())
{
var director = gr.u_director;
gs.log("the Group name" + gr.getDisplayValue('group'));
gs.log("Is director true or false" + director );
if(gr.getDisplayValue('group') == 'Director Group' && director == 'true')

{

return 'yes';
}
return 'no';
}
}

 

Regards

Arjun

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Arjun,


Can you try the following script. 

answer = ifScript();

function ifScript() {
varusr=current.variables.requested_for; //Requsted for user
vargr=newGlideRecord('sys_user_grmember');
gr.addEncodedQuery('user='+usr+'^group.name=Director Group');
gr.query();
if (gr.next()) {
return'yes';
}
return'no';
}
 
Please mark the answer helpful or correct based on the impact of the answer.
 
Thanks
Ishan Parikh

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hi Arjun,


Can you try the following script. 

answer = ifScript();

function ifScript() {
varusr=current.variables.requested_for; //Requsted for user
vargr=newGlideRecord('sys_user_grmember');
gr.addEncodedQuery('user='+usr+'^group.name=Director Group');
gr.query();
if (gr.next()) {
return'yes';
}
return'no';
}
 
Please mark the answer helpful or correct based on the impact of the answer.
 
Thanks
Ishan Parikh

Hi Ishan, 

 

Thanks for the help on this, 

As I'm new to ServiceNow it will be very helpful if you kindly help with this part of the query,

gr.addEncodedQuery('user='+usr+'^group.name=Director Group');

 

Regards

Arjun

Community Alums
Not applicable

Hi Arjun, 

What help do you need from that line of the code ? 

If you mean helping you understand - it does the following 

The addEncodedQuery basically builds up the filter that you would like to apply on the glide record (table). 

the two attributes that you have on the group membership table are 

1) user 2) group 

you want that the requested for user must be member of a particular group (Director group) and if it is true then return yes 

so in this we are building that query 

'user=' --> is the attribute on the membership table.

+usr+ --> dynamic value of the requested for user value.

'group.name=Director Group' --> search for the group name value (exact match with Director Group) value. 

Thanks
Ishan Parikh

 

Omkar Mone
Mega Sage

Hi 

Please try the below script :- 

 

var arr=[];
var usr=current.variables.requested_for;
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', usr);
gr.query();
while(gr.next())
{
arr.push(gr.sys_id.toString());
}
for(var j=0;j<arr.length;j++)
{
  checkForEveryGroup(arr[j]);
}


function checkForEveryGroup(eachGroup)
{
//here check your condition
//each group has now the groups in which the user is in. It will call this function for every iteration and do you checkinng.


}