How to list out the users with no roles ?

Tejas16
Tera Contributor

How to list out the users with no roles just like (Abel Tuter) in servicenow Learning Platform?

1 ACCEPTED SOLUTION

Sulabh Garg
Mega Sage
Mega Sage

Hello Tejas,

you can use the below fix script to get the users list who do not have any roles

 

var gr3 = new GlideRecord('sys_user');

gr3.query();

while(gr3.next())

{

var gr2 = new GlideRecord('sys_user_has_role');

gr2.addQuery('user',gr3.sys_id);

gr2.query();

if(!gr2.next())

{

gs.print('User Name is(No role) : ' + gr3.name);

}

}

Please Mark ✅ Correct/helpful, if applicable, Thanks!! 

Regards

Sulabh Garg

Please Mark ✅ Correct/helpful, if applicable, Thanks!!
Regards
Sulabh Garg

View solution in original post

7 REPLIES 7

Hi Tejas, I see you have marked the other response as correct, Did you face any issue with this script?

Can you please mark my response as correct, If applicable. Thanks

Please Mark ✅ Correct/helpful, if applicable, Thanks!!
Regards
Sulabh Garg

Done

Community Alums
Not applicable

You can improve it by adding the addActivequery

var gr3 = new GlideRecord('sys_user');
gr3.addActivequery()
gr3.query();
while(gr3.next())
{
var gr2 = new GlideRecord('sys_user_has_role');
gr2.addQuery('user',gr3.sys_id);
gr2.query();
if(!gr2.next())
{
gs.print('User without roles name :  ' + gr3.name);
}
}