- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2025 07:37 AM
I have created the reference qualifier visible in the screenshot to only display users where the company is the same as for the logged in user. Unfortunately it does not work, it still displays all users from sys_user:
Full javascript: javascript:gs.getUser().getCompanyID()
Any ideas what could be the issue?
Any ideas how to best debug this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2025 01:36 AM
Thanks all for the extensive information. I made it work now with a dynamic filter option calling an Ajax Script include which has a function retrieving the Company ID. Marked the option as solution which was closest to mine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2025 08:47 AM
Hi @Julia Baus ,
Try this:
Try logout and login if gs.getUser().getCompanyID() is returning empty value (Verify in background script).
Thanks
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2025 12:43 AM
I tried that and check also via background script that function returns a proper value, but still it doe not work and I get all users as result in the variable. I tested it with my admin, so also missing roles shouldn't be any issue
Any idea what could be wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2025 09:52 AM
Hi @Julia Baus ,
I have tested the below code in its working fine in PDI, Please review -
Reference Qualifier- " javascript: new LoggedInUserUtils().getCompanyUser(gs.getUserID()); "
and Script Include-
If you found my response helpful, please mark it as helpful and accept it as the solution for Future Readers
Thank you
Nawal Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2025 10:01 AM
ServiceNow – Reference Qualifier Filtering Users by Company (Debug and Fix)
Issue:
A reference qualifier was created to only display users where the company matches the logged-in user's company, but it still shows all users. The current code was:
javascript: gs.getUser().getCompanyID()
Root Cause:
The issue is that the reference qualifier must return an **encoded query string**, not just a sys_id. Returning a sys_id by itself does not create a filter condition, so the system defaults to showing all users.
---
✅ Correct Reference Qualifier (Advanced)
Use this instead:
javascript: 'company=' + gs.getUser().getCompanyID()
Optional (to show only active users):
javascript: 'active=true^company=' + gs.getUser().getCompanyID()
This returns a valid encoded query string that filters the sys_user table by the logged-in user's company.
---
If used in a Service Catalog variable:
The same syntax works, but if you prefer better structure, use a client-callable Script Include.
Reference Qualifier (Advanced):
javascript: new MyRefQual().usersInMyCompany();
Script Include:
var MyRefQual = Class.create();
MyRefQual.prototype = Object.extendsObject(AbstractAjaxProcessor, {
usersInMyCompany: function () {
var cid = gs.getUser().getCompanyID();
if (!cid) return 'sys_idISEMPTY';
return 'active=true^company=' + cid;
},
isPublic: function() { return true; }
});
---
Common Pitfalls:
1. Ensure the qualifier type is “Advanced.”
2. The target field is company on the sys_user table.
3. The logged-in user must have a company value.
4. In Workspace or Service Portal, ensure compatibility mode preserves ref quals.
5. Clear cached choices after updating (hard refresh or clear cache).
---
Debugging Tips:
- Hardcode a known company sys_id to test:
javascript: 'company=46d44a40c0a8010e01b93d1e6bba8f4a'
- If that works, the issue is in how gs.getUser().getCompanyID() returns or formats.
- Check system logs (System Logs > All) with gs.info() for returned values.
- Test the resulting query in a list view:
/sys_user_list.do?sysparm_query=company=<sys_id>
---
✅ TL;DR
The qualifier must return an encoded query string, not just a sys_id.
Working example:
javascript: 'company=' + gs.getUser().getCompanyID()
This reliably filters users to those whose company matches the logged-in user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2025 12:48 AM
