How to get all the contains role of a role?

Susmitha14
Tera Contributor

Hi,

I want to write a script to get contains roles of a role. for example I want to get  all the contains role of an admin role

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Susmitha,

there are couple of approaches

1) from UI

2) from script

1) From UI

a) go to table "sys_user_role_contains"

b) then search with role as "admin"

find_real_file.png

2) From Background Script/Fix Script:

getRolesName();

function getRolesName(){
 
 var roleName = 'admin';
 var arr = [];
 var containsRecord = new GlideRecord('sys_user_role_contains');
 containsRecord.addQuery('role.name', roleName);
 containsRecord.query();
 while(containsRecord.next()) {
     arr.push(containsRecord.contains.name.toString());
 }

gs.info('Contains roles are:'+arr);

}

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Hi Ankur,

Can you please explain it through script .

Hi Sumitha,

sharing few links which would guide you for recursion function

https://community.servicenow.com/community?id=community_question&sys_id=c01f6d68db734c941cd8a345ca96...

https://community.servicenow.com/community?id=community_question&sys_id=caa14b69db98dbc01dcaf3231f96...

https://community.servicenow.com/community?id=community_question&sys_id=bf44047cdb98a308a39a0b55ca96...

Regards
Ankur

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

Hi Sumitha,

please find below script which will give all roles if you give "sn_hr_core.basic"

Script: try this in scheduled job or fix script and not scripts background

var arr = ['sn_hr_core.basic']; // name of role

var values = getChildRoles('sn_hr_core.basic', arr);

gs.info("Roles are : " + values);

function getChildRoles(role,arr)
{
	var finalArr = [];
	finalArr = arr;
	var gr = new GlideRecord('sys_user_role_contains');
	gr.addQuery('role.name', role);
	gr.query();
	while(gr.next()){
		finalArr.push(gr.contains.name.toString());
		if (gr.contains !=''){
			this.getChildRoles(gr.contains.name.toString(),finalArr);
		}
	}
	return finalArr;
}

Output: It gave me these roles

find_real_file.png

Regards
Ankur

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

Muralidharan BS
Mega Sage
Mega Sage

HI Susmitha, 

you can use this script below which will give you all the inherited roles. 

var ci = [];
var x = 'asset';
var gr = new GlideRecord('sys_user_role_contains');
gr.addQuery('role.name='+x);
gr.query();
while(gr.next())
{
var getParent = gr.contains;
ci.push(getParent);
for ( var i=0;i<ci.length;i++)
{
var gr1 = new GlideRecord('sys_user_role_contains');
gr1.addQuery('role',ci[i]);
gr1.query();
while(gr1.next())
{
var getParent1 = gr1.contains;
ci.push(getParent1);
ci = ci.filter( function( item, index, inputArray ) {
           return inputArray.indexOf(item) == index;
});
}
}
for ( var i=0;i<ci.length;i++){
var gr2 = new GlideRecord('sys_user_role_contains');
gr2.addQuery('role',ci[i]);
gr2.query();
while(gr2.next())
{
gs.info(gr2.contains.getDisplayValue());
}}
}

 

Kindly mark my response correct and helpful if my suggestion resolved your query,


Thanks

Murali