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

Muralidharan BS
Mega Sage
Mega Sage

HI Susmitha,

Kindly give a try,

 var record = new GlideRecord('sys_user_role_contains');
 record.addQuery('role.name=admin');
 record.query();
 while (record.next()) {
     gs.info(record.contains.getDisplayValue());

 }

 

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


Thanks

Murali

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

Hi Ankur,

This script is working.But I want to get if sn_hr_core,basic is the role it contains so many other roles like sn_hr_core.case_writer. If I am adding basic role to a user it is also getting sn_hr_core.case_Reader also adding because case_writer contains case_Reader role. So is there any way to get all the roles when we add a role to the user.(when contains role contains other roles also)

Hi Susmitha,

in that case you will have to perform recursive function call and pass the 1st role name

It would give all roles from that level to the last

Regards
Ankur

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