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.

Fix Script Role contains Roles, problem in views.

juancarrion
Tera Contributor

Hello Community, 

I have a problem in my current development that I couldn't crack.

1. I want to find all the users which has the view_changer role.
2. Problem is that view_changer role is contain in several roles, groups,etc.
Bottom line how can I find all the roles and group that contains view_changer role?

because thinking itil has the view_changer and admin has itil so at the end both admin and itil have the view_changer is there any easy way to do this on ServiceNow?


2 ACCEPTED SOLUTIONS

JenniferRah
Mega Sage
Mega Sage

I just ran this and it seemed to work:

var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.query();
var count = 0;
var x = gr.getRowCount();
var curuser = gs.getUser();
while(gr.next()){
	var user = curuser.getUserByID(gr.user_name);
	if(user.hasRole('view_changer')){
		gs.print("** " + gr.name + " has role");
		count++;
	}
}
gs.print("** Found " + count + " users out of " + x + " with role.");

View solution in original post

jcmings
Mega Sage

Find all roles that contain view_changer

Navigate to sys_user_role_contains.list and search for the role view_change

 

Find all groups that contain view_changer

Navigate to sys_group_has_role.list and search for the role view_change OR whatever roles you identified that contain the role

 

Find all users that have the view_change role

Navigate to sys_user_has_role.list and search for the role view_change OR whatever roles you identified that contain the role

View solution in original post

5 REPLIES 5

JenniferRah
Mega Sage
Mega Sage

I just ran this and it seemed to work:

var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.query();
var count = 0;
var x = gr.getRowCount();
var curuser = gs.getUser();
while(gr.next()){
	var user = curuser.getUserByID(gr.user_name);
	if(user.hasRole('view_changer')){
		gs.print("** " + gr.name + " has role");
		count++;
	}
}
gs.print("** Found " + count + " users out of " + x + " with role.");

jcmings
Mega Sage

Find all roles that contain view_changer

Navigate to sys_user_role_contains.list and search for the role view_change

 

Find all groups that contain view_changer

Navigate to sys_group_has_role.list and search for the role view_change OR whatever roles you identified that contain the role

 

Find all users that have the view_change role

Navigate to sys_user_has_role.list and search for the role view_change OR whatever roles you identified that contain the role

Thanks a lot it was very helpful in my final solution.

AshishKM
Kilo Patron
Kilo Patron

Hi @juancarrion , 

You need to fetch data from tables and then filter based on need. 

 

Get the list of role(s) which contains this view_change role from table "sys_user_role_contains

AshishKM_0-1739328254804.png

 

Get the list of User who has this role, either direct or through group and inherited & inheritence count will explain that. You will get the Contained By  and Group both the count here.

 

AshishKM_2-1739328451778.png

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution