Reference Fields - Filtering
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 11:37 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 12:23 PM
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:
- Navigate to System Definition → Dictionary
- Find the field where you want to apply the filter
- In the Reference Specification section, locate the Reference Qualifier field
- 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:
- In the field’s Dictionary entry, set the Reference Qualifier type to Dynamic
- Create a Scripted Reference Qualifier in System Definition → Script Includes
- 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:
- Go to System UI → Dictionary
- Find the field and modify the "Display Value"
- 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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2025 02:51 PM
Hi @Jonathan102
Try this with No Code. Go to the Field dictionary and then add these conditions.
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]
****************************************************************************************************************