GlideRecord returns only the first record from the query.

msm4
Mega Guru

Hello Team,

I am trying to query the group member table and applied few conditions to check if user is only member one group.

here, the expecting output is list of all the users member of only a single group, to achieve this I have written a script include function and calling that in report filter, but my code is returning only the first record. Please let me know where I am going wrong. How do we get all the records comma separated when we return the array.

Thanks.

noAGITIL: function() {
        var grm = new GlideAggregate('sys_user_grmember');
grm.addEncodedQuery('group.typeLIKE3d033f994f861600bc65d0af0310c797^ORgroup=e3bc830a4fcb9200bc65d0af0310c746');
grm.addAggregate('COUNT');
grm.orderByAggregate('COUNT');
grm.groupBy('user');
grm.query();
while (grm.next()){
var roleCount = grm.getAggregate('COUNT');
if(roleCount==1)
{
var roleGr = new GlideRecord('sys_user_grmember');
roleGr.addQuery('user', grm.user);
roleGr.query();
var answer = new Array();
while(roleGr.next())
{
      if(roleGr.group == 'e3bc830a4fcb9200bc65d0af0310c746')
       {
           answer.push(roleGr.user.toString());
       }
	
}
	return answer;
}
}
	},

 

8 REPLIES 8

Maik Skoddow
Tera Patron

Hi @msm 

the reason is the aggregate function. This will reduce the results if "grm" to one entry:

grm.addAggregate('COUNT');
grm.orderByAggregate('COUNT');

Remove this and your code should work as expected.

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Hi Maik,

Thanks for the response,

reason I have added them, to know the count of the user groups, if I remove them, I cannot find the users who are members of only one group.

I have ran the same script using back ground script, I have got all the results, Please find the below output

find_real_file.png

 

Please suggest!

 

 

Hi

you can use the following script as starting point

var gr = new GlideAggregate('sys_user_has_role'); 
gr.addAggregate('count');
gr.orderByAggregate('count');
gr.groupBy('user');
gr.query();

while(gr.next()){
    var roleCount = gr.getAggregate('count');

    if(roleCount == 1){
      
      //add your code here

    }	
}

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Is there any way where I can get the array output a comma separated?