How to Glide users which are not belong to any group

Shaik nabi khaj
Tera Expert

 

Hi,

I have a requirement as follow below.

There are some users in sys_user table which do not have any group and role.

Now how i can glide those users by scripting.

Please provide ur inputs.

@Ankur Bawiskar 

@Taha Habib 

@Gunjan Kiratkar 

 

Best Regards,

Shaik Nabi khaja. 

3 REPLIES 3

Community Alums
Not applicable

Taha Habib
Tera Guru

Hey Shaik Nabi,

 

Use the following script to achieve your requirement:

Here I am using three tables:

1. sys_user : Table for all users

2. sys_user_grmember : Table for users and there groups. Only users with group are there in this table.

3 sys_user_has_role: Table for users and there roles. Only users with roles are there in this table.

 

var userMember=[];
var userwithnorolegroup=[];
var urole=0;
var count=0;
var members=0;
var norolegroup=0;
var userslist= new GlideRecord('sys_user');
userslist.query();
while(userslist.next())
{
  var grpMember = new GlideRecord('sys_user_grmember');
  grpMember.addQuery('user.sys_id',userslist.sys_id);
  grpMember.query();
  if(grpMember.next())
   {
         
         members++;
   }
  else
   {
        count++;
        userMember.push(userslist.sys_id.toString());
        
        var userrole= new GlideRecord('sys_user_has_role');
        userrole.addQuery("user.sys_id", userslist.sys_id);
        userrole.query()
        if(userrole.next())
        {
            urole++;
        }
        else
        {
              norolegroup++;
              userwithnorolegroup.push(userslist.sys_id.toString());
        }
   } 
}
gs.info(" sys_id of users with no group:")
gs.info(userMember);
gs.info("count of user who doesn't belong to any group: "+ count);
gs.info("count of user who belong to any group: "+ members);
gs.info("count of user who belong to no group but have a role: "+ urole);
gs.info("count of user who belong to no group and who do not even have a role: "+ norolegroup);

gs.info("sys_id of users with no group or role:");
gs.info(userwithnorolegroup);

 

I hope this helps you, if you find this helpful. Please mark this as helpful and correct. Thank you.

Ankur Bawiskar
Tera Patron
Tera Patron

@Shaik nabi khaj 

what did you start and where are you stuck?

If you start from your side it will be learning for you as well.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader