- 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-15-2020 01:17 AM
Hi Ankur,
Can you please explain it through script .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2020 01:36 AM
Hi Sumitha,
sharing few links which would guide you for recursion function
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 02:01 AM
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
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 01:54 AM
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