How to Return only results matching a user's Department in a Reference Field Variable

mikeinsley
Kilo Contributor

Hi

I have a variable set that includes a Line Manager field. This Variable is a reference field, looking at the sys_user table. I would like to be able to filter the results returned in the reference field based on the logged in User's current department. Under Type Specifications I've added reference qualifier conditions of:

  • Active = True
  • Company is (dynamic) My Company
  • Department = Department

However, this set of filters is returning all users matching the logged in user's Company, but is ignoring Department.  

Would anyone know of a way to do this?

Thanks

Mike

1 ACCEPTED SOLUTION

ghsrikanth
Tera Guru

Hi Mike,



It is good and easy to use the filtered conditions like above, but it is usually good to go for a script include and function in the reference qualifier - in the condition.


The most important advantage of this kind of scripting is reusability and if any need of modification, you need to modify at one place and it gets reflected in all the other places. Its the best practice to do also.


Reference Qualifiers - ServiceNow Wiki



So in your case -


In the reference field's reference qualifier write the below code -


javascript:UserUtil.getDepartmentUsers();



Screen Shot 2016-03-10 at 7.51.37 PM.png



Create a new script include:


var UserUtil = Class.create();


UserUtil.prototype = {


      initialize: function() {


      },



  getDepartmentUsers:function(){


  var usersList = "sys_idIN";


  var grUser = new GlideRecord('sys_user');


  grUser.addQuery('company',gs.getUser().getCompanyID());


  grUser.addQuery('department',gs.getUser().getDepartmentID());


  grUser.query();



  while(grUser.next()){


  usersList = usersList + ','+grUser.sys_id;


  }


  },



      type: 'UserUtil'


};




Mark if it is helpful or correct, feedback is appreciated


View solution in original post

7 REPLIES 7

Harish Murikina
Tera Guru

Hi Mike,



Give this line in your reference qualifier.



javascript: gs.getUser().getDepartmentID()



Regards,


Harish Murikinati.


Thanks for your help, this worked well.


ghsrikanth
Tera Guru

Hi Mike,



It is good and easy to use the filtered conditions like above, but it is usually good to go for a script include and function in the reference qualifier - in the condition.


The most important advantage of this kind of scripting is reusability and if any need of modification, you need to modify at one place and it gets reflected in all the other places. Its the best practice to do also.


Reference Qualifiers - ServiceNow Wiki



So in your case -


In the reference field's reference qualifier write the below code -


javascript:UserUtil.getDepartmentUsers();



Screen Shot 2016-03-10 at 7.51.37 PM.png



Create a new script include:


var UserUtil = Class.create();


UserUtil.prototype = {


      initialize: function() {


      },



  getDepartmentUsers:function(){


  var usersList = "sys_idIN";


  var grUser = new GlideRecord('sys_user');


  grUser.addQuery('company',gs.getUser().getCompanyID());


  grUser.addQuery('department',gs.getUser().getDepartmentID());


  grUser.query();



  while(grUser.next()){


  usersList = usersList + ','+grUser.sys_id;


  }


  },



      type: 'UserUtil'


};




Mark if it is helpful or correct, feedback is appreciated


Thanks for the help, this worked well