- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 10:45 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 11:01 PM
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"
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 10:53 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 11:01 PM
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"
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2020 12:37 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2020 12:52 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader