The CreatorCon Call for Content is officially open! Get started here.

Advanced Reference Qualifier for Specific Role

AndresGT1
Giga Expert

Hello,

I'm barely new to Reference Qualifiers and don't know if there's a way to filter the users table and only show the users that have the role "business_partner"

This is what I found when I searched for something:

javascript:"sys_idIN"+getRoledUsers("itil_admin").join(",")

But I really don't know how it works, that qualifier brings me all users that have at least one role from parent role to child roles

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

getRoledUsers is an OOTB script (see Business Rule getRoledUsers)



// Return an array of sys_ids of the users that have at least one role


// optional parameters allow the exclusion (NOT IN) of some roles or


// look for specific roles (IN)


//


// optional: queryCondition - 'IN' or 'NOT IN'


// optional: roleList - a comma separated list of role names


//


function getRoledUsers(queryCondition, roleList) {


  var roleListIds;


  if (queryCondition && roleList) {


          roleListIds = getRoleListIds(roleList);


  }




  var users = {};


  var gr = new GlideRecord('sys_user_has_role');


  if (roleListIds) {


          gr.addQuery('role', queryCondition, roleListIds);


  }


  gr.query();


  while (gr.next()) {


          users[gr.user.toString()] = true;


  }



  var ids = [];


  for (var id in users)


          ids.push(id);


       


  return ids;


}



So, the syntax your are looking for is



javascript:'sys_idIN' + getRoledUsers('IN','itil_admin').join(",");



No need for custom code



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

5 REPLIES 5

amlesh
Kilo Sage

Would you like to do this.



Script Include:


Name: RoleUtilities


Script:


var RoleUtilities = Class.create();


RoleUtilities.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getRoledUsers : function(){


var arr=[];


var gr = new GlideRecord('sys_user_has_role');


gr.addQuery('role.name','itil_admin');


gr.query();


while(gr.next()){


arr.push(gr.user);


}


return 'sys_idIN'+arr;


},


      type: 'RoleUtilities'


});



Reference Qualifier:



javascript: new RoleUtilities().getRoledUsers();


This code will help me in the future, thanks a lot! (:


The SN Nerd
Giga Sage
Giga Sage

getRoledUsers is an OOTB script (see Business Rule getRoledUsers)



// Return an array of sys_ids of the users that have at least one role


// optional parameters allow the exclusion (NOT IN) of some roles or


// look for specific roles (IN)


//


// optional: queryCondition - 'IN' or 'NOT IN'


// optional: roleList - a comma separated list of role names


//


function getRoledUsers(queryCondition, roleList) {


  var roleListIds;


  if (queryCondition && roleList) {


          roleListIds = getRoleListIds(roleList);


  }




  var users = {};


  var gr = new GlideRecord('sys_user_has_role');


  if (roleListIds) {


          gr.addQuery('role', queryCondition, roleListIds);


  }


  gr.query();


  while (gr.next()) {


          users[gr.user.toString()] = true;


  }



  var ids = [];


  for (var id in users)


          ids.push(id);


       


  return ids;


}



So, the syntax your are looking for is



javascript:'sys_idIN' + getRoledUsers('IN','itil_admin').join(",");



No need for custom code



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

This is exactly what I needed for this requirement.



Would it be too much if I asked you what difference does it make by adding the "IN" into the script?