Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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

sonali panda1
Kilo Sage

Hi,

Check this -

https://community.servicenow.com/community?id=community_article&sys_id=7c00ca90db5ec450d58ea345ca9619c5

 

Or in script form something like this -

var ans=[];
var user = new GlideRecord('sys_user');
user.addEncodedQuery('active=true^sys_id!=javascript:getRoledUsers();');
user.query();
while(user.next())
{
ans.push(user.name+''); // to get list of user names . else pass user.sys_id to get sysids
}
gs.info(ans.toString());

Thank You.

Sulabh Garg
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

Thank You.