Show or hide the names in a sys_user reference field based on group membership

alexlazo
Mega Contributor

I'm trying to create a reference qualifier or script that will allow me to filter out users in the sys_user table by the group membership related list. So if they belong to a specific group, remove them from the available options in the reference field. I see its easy to do it by role but I can't seem to do it by group.

1 ACCEPTED SOLUTION

Here is a description of how to script reference qualifiers, just use script include instead of business rule:


Reference Qualifiers - ServiceNow Wiki



Okay, so to show the full example, this is how it might look.


Script include:


var UserFilterUtils = Class.create();


UserFilterUtils.prototype = {


  initialize: function() {},



  filterByGroup: function(group_id) {


  //Will show only users who are members of the specified group


  var found_users = [];



  var gr = new GlideRecord("sys_user_grmember");


  gr.addQuery("group", group_id);


  gr.addNotNullQuery("user");


  gr.query();


  while (gr.next()) {


  found_users.push(gr.user.sys_id.toString());


  }


  var ref_qual = "sys_idIN" + found_users.join(",");


  return ref_qual;


  },



  filterOutByGroup: function(group_id) {


  //Will remove users of the specified group from the list


  var found_users = [];



  var gr = new GlideRecord("sys_user_grmember");


  gr.addQuery("group", group_id);


  gr.addNotNullQuery("user");


  gr.query();


  while (gr.next()) {


  found_users.push(gr.user.sys_id.toString());


  }


  var ref_qual = "sys_idNOT IN" + found_users.join(",");


  return ref_qual;


  },



  type: 'UserFilterUtils'


};



Reference qualifier on the user reference field:


javascript:(new UserFilterUtils()).filterOutByGroup("group_sys_id");


Or if you need to filter by the group that is selected on the form:


javascript:(new UserFilterUtils()).filterOutByGroup(current.field_on_the_form);


View solution in original post

7 REPLIES 7

Thank you Kyryl!


Thanks so much, I've been trying for hours to figure out how to do this, and this worked perfectly.

Hi kyryl ,

 

I have one requirement, I have one field called "user" which is reference field and there are two users belongs to one group(GLOBAL_FINANACVE). For one user we need to show only 40 users and for another we need to show all users. for that i wrote reference Qualifier but it is not working