Reference Fields - Filtering

Jonathan102
Tera Guru

Hi all,

 

Does anyone know how to add a filter to a reference field like the image I have attached?  I need to be able to restrict inactive accounts and test accounts but don't know how to in this type of interface.  It would even work if we could find a way to display the corresponding user ID beside of the name in the selection list.

 

Thanks,

Jonathan 

2 REPLIES 2

Itallo Brandão
Kilo Guru

 

Hi Jonathan,

To filter a reference field in ServiceNow and restrict specific accounts (e.g., inactive users and test accounts), you typically have a few options depending on how the field is set up:


1. Using the "Advanced Reference Qualifier" in the Dictionary

If this is a reference field on a form, you can add a filter by doing the following:

  1. Navigate to System Definition → Dictionary
  2. Find the field where you want to apply the filter
  3. In the Reference Specification section, locate the Reference Qualifier field
  4. Use an Encoded Query to restrict the results. For example:
     

 

active=true^user_nameDOES NOT CONTAIN test​

 

  • This filters out inactive users (active=false)
  • Excludes accounts where the user_name contains "test"

2. Using a Reference Qualifier Script (Dynamic Filtering)

If you need more complex logic, use Dynamic Filters:

  1. In the field’s Dictionary entry, set the Reference Qualifier type to Dynamic
  2. Create a Scripted Reference Qualifier in System Definition → Script Includes
  3. Example Script Include:
     

 

var CustomUserFilter = Class.create();
CustomUserFilter.prototype = {
    initialize: function() {},
    getUserList: function() {
        var users = [];
        var gr = new GlideRecord('sys_user');
        gr.addQuery('active', true);  // Only active users
        gr.addQuery('user_name', 'DOES NOT CONTAIN', 'test'); // Exclude test accounts
        gr.query();
        while (gr.next()) {
            users.push(gr.sys_id.toString());
        }
        return users;
    }
};​

 

  • In the Reference Qualifier of your field, use:
     

 

javascript:CustomUserFilter.getUserList()​

 


3. Showing User ID Beside the Name

If you want the User ID to appear next to the Name in the selection list:

  1. Go to System UI → Dictionary
  2. Find the field and modify the "Display Value"
  3. Change the "Display Field" in sys_user to show user_name + sys_id (or use a Display Business Rule)

Example Display Business Rule:

 

(function executeRule(current, gForm, gSNC) {
    current.name = current.name + " (" + current.user_name + ")";
})(current, gForm, gSNC);

 


Conclusion

  • Use a Reference Qualifier for a basic filter (active=true^user_nameDOES NOT CONTAIN test)
  • Use a Scripted Reference Qualifier if you need dynamic conditions
  • Modify the Display Field or add a Business Rule if you want User ID beside the name

If this answer was helpful, please like and mark it as useful—this helps me and the community! 🚀

 

Let me know if you need help implementing it! 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Jonathan102 

 

Try this with No Code. Go to the Field dictionary and then add these conditions.

 

 

AGLearnNGrow_0-1741729884536.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************