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

Hi Mike,


If the answer solved your issue, could you please mark the answer has correct to help the future users of community,



Thanks,


Is it possible to filter the reference filed based on the current loggedin user role

Raja Mohsin1
Tera Contributor

Well instead of creating the script include and then calling it from the reference qualifier.

I would suggest to add this in reference qualifier and you can active if you want to include inactive users aswell

javascript:'active=true^department='+gs.getUser().getDepartmentID();