Get users sysids from group memeber table

Vinay49
Tera Expert

Hello,

I am trying to get users sys_id's from sys_user_grmember table for one particular group(X). (X) group have 13 group members. I have written a code. however It seems code is not working. It prints only one sysid multiple times.

           var arryVal = [];
            var gr = new GlideRecord('sys_user_grmember');
            gr.addQuery('group', '6966ec031b040594e75ef0e4464bcb8c');//sysid of group(X)
            gr.setLimit(5);
            gr.query();
            while (gr.next()) {
                arryVal.push(gr.user);
            }
          gs.print('sys_idIN'+arryVal.join(','));
 
 
It print the output as : 
sys_idIN0b41c7dbdbf007000fc9ff7dae9619fd,0b41c7dbdbf007000fc9ff7dae9619fd,0b41c7dbdbf007000fc9ff7dae9619fd,0b41c7dbdbf007000fc9ff7dae9619fd,0b41c7dbdbf007000fc9ff7dae9619fd
3 ACCEPTED SOLUTIONS

Sumanth16
Kilo Patron

Hi @Vinay49 ,

 

var users = [];

var gr = new GlideRecord('sys_user_grmember');

gr.addEncodedQuery("group=b8c3923613038b0014145a132244b05a");//update sys_id

gr.query();

gs.print("query success");

while(gr.next())

{

users.push(gr.user+'');//your code change to this line

}

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

View solution in original post

Aman Kumar S
Kilo Patron

HI @Vinay49 

Change this one line in while loop as:

arryVal.push(gr.user.toString());

 

The reason is, when you are using (gr.user), its putting the GlideElement object into the array instead of the actual value so it changes as you loop through the records, becoming the value of the next record as you loop. To add the real "value" of the field to the Array, use the above line with toString() to get the actual value.

 

Best Regards
Aman Kumar

View solution in original post

Harish KM
Kilo Patron
Kilo Patron

Hi @Vinay49 always use getValue like below, corrected your code

   var arryVal = [];
            var gr = new GlideRecord('sys_user_grmember');
            gr.addQuery('group', '6966ec031b040594e75ef0e4464bcb8c');//sysid of group(X)
            gr.setLimit(5);
            gr.query();
            while (gr.next()) {
arryVal.push(gr.getValue('user')); //use getValue, since your using gr.user it returns same sysid or you need to use gr.user.toString()
    }
          gs.print('sys_idIN'+arryVal.join(','));

 

Regards
Harish

View solution in original post

3 REPLIES 3

Sumanth16
Kilo Patron

Hi @Vinay49 ,

 

var users = [];

var gr = new GlideRecord('sys_user_grmember');

gr.addEncodedQuery("group=b8c3923613038b0014145a132244b05a");//update sys_id

gr.query();

gs.print("query success");

while(gr.next())

{

users.push(gr.user+'');//your code change to this line

}

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

Aman Kumar S
Kilo Patron

HI @Vinay49 

Change this one line in while loop as:

arryVal.push(gr.user.toString());

 

The reason is, when you are using (gr.user), its putting the GlideElement object into the array instead of the actual value so it changes as you loop through the records, becoming the value of the next record as you loop. To add the real "value" of the field to the Array, use the above line with toString() to get the actual value.

 

Best Regards
Aman Kumar

Harish KM
Kilo Patron
Kilo Patron

Hi @Vinay49 always use getValue like below, corrected your code

   var arryVal = [];
            var gr = new GlideRecord('sys_user_grmember');
            gr.addQuery('group', '6966ec031b040594e75ef0e4464bcb8c');//sysid of group(X)
            gr.setLimit(5);
            gr.query();
            while (gr.next()) {
arryVal.push(gr.getValue('user')); //use getValue, since your using gr.user it returns same sysid or you need to use gr.user.toString()
    }
          gs.print('sys_idIN'+arryVal.join(','));

 

Regards
Harish