- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
3 weeks ago
ServiceNow – Reference Qualifier: Show only users from the same Company as the logged‑in user
Problem
You set a reference qualifier to limit a user reference to users in the same Company as the current user using:
javascript:gs.getUser().getCompanyID()
…but the field still lists all users.
Why this happens (common causes)
1) Wrong return type: Advanced ref qualifiers must return an **encoded query string** (e.g., 'company=<sys_id>'), not a raw sys_id.
2) Admin has no Company: If your test user (often 'admin') has no Company set, the qualifier becomes empty and returns **all** users.
3) Catalog variables / Portal / Workspace: Client-side contexts (SP/UI Builder) can’t evaluate server-only gs calls directly. Use a **Dynamic Filter Option** or **client-callable Script Include** instead.
4) List Collector vs Reference: List Collectors have their own qualifier behavior; make sure you set the qualifier on the **variable**, not just the dictionary field.
5) Caching: Old qualifiers can be cached; a hard refresh or re-open is needed after changes.
Three reliable solutions
A) Zero‑code: Dynamic Filter Option (works platform + portal)
1. System Definition → Dynamic Filter Options → New
- Table: sys_user
- Field: company
- Operator: is
- Value: javascript:gs.getUser().getCompanyID()
- Name: Users in my company
2. On the reference field/variable set Reference qualifier = DYNAMIC: Users in my company
B) Advanced qualifier (server) — correct return value
Reference qualifier (Advanced):
javascript:
(function(){
var c = gs.getUser().getCompanyID();
if (!c) return 'sys_id=javascript:gs.nil("block_all")'; // prevent all users if no company
return 'company=' + c;
})();
Notes:
- Must return an encoded query string.
- Add a restrictive fallback when Company is empty.
C) Client‑callable Script Include (works in SP/Workspace)
Script Include (Client callable = true):
var UserFilterAjax = Class.create();
UserFilterAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
byMyCompany: function() {
var c = gs.getUser().getCompanyID();
if (!c) return 'sys_id=javascript:gs.nil("block_all")';
return 'company=' + c; // encoded query string
},
type: 'UserFilterAjax'
});
Reference qualifier (Advanced) on the variable:
javascript:new UserFilterAjax().byMyCompany()
Extra tips / debugging checklist
- Verify the **Company** field on your test user is populated.
- Test the encoded query in a list: open sys_user list, add filter 'company=<sys_id>' — confirm it returns the expected subset.
- For Portal, ensure the Script Include is in the same scope as the variable (or use x_scope.ClassName).
- For List Collector variables:
- Set the qualifier on the **List Collector variable** itself.
- Optionally set attribute `ref_qual_elements=company` if you want the qualifier to refresh when related fields change.
- Turn on debug: add a temporary gs.log in the Script Include to log the computed company and the encoded query.
- Clear cache where needed: try a new incognito window to avoid stale client cache.
Minimal working examples
1) Dynamic: DYNAMIC: Users in my company
2) Advanced:
javascript:(function(){ var c=gs.getUser().getCompanyID(); return c ? 'company='+c : 'sys_id=javascript:gs.nil("block_all")'; })();
Conclusion
- Ensure the qualifier **returns an encoded query**, not just a sys_id.
- Don’t test with an admin that has no Company; add a fallback to avoid “all users”.
- For Portal/Workspace, prefer Dynamic Filter Options or a **client‑callable Script Include**.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Quick checks
Are you filtering a catalog variable?
Catalog variables evaluate ref quals differently than table fields. Don’t rely on gs in the ref qual directly—use a client-callable Script Include (AJAX) or a Dynamic Reference Qualifier.
Admin test user
Your admin might have no Company set. If your function returns an empty string, ServiceNow shows all users. Return a restrictive fallback (e.g., sys_id=javascript:gs.nil('x')) when company is empty.
List Collector vs Reference
For List Collector variables you must set the List collector’s Reference qualifier (and often the ref_qual_elements attribute) — setting it on the underlying dictionary field won’t apply.
Portal/Workspace
In Service Portal / UI Builder, make sure you use client-callable Script Includes. Pure server-side gs.getUser().getCompanyID() directly in the ref-qual can be ignored on the client.
Two solid ways that work
A) Dynamic reference qualifier (zero code)
Create a Dynamic Filter Option:
System Definition → Dynamic Filter Options → New
Table: sys_user
Name: Users in my company
Filter: company = javascript:gs.getUser().getCompanyID()
On your variable (Type = Reference to sys_user), set Reference qualifier to:
DYNAMIC: Users in my company
Works both in platform UI and portal and is upgrade-safe.
B) Client-callable Script Include (works for portal/workspace)
Script Include (server, Client callable = true😞
var UserFilterAjax = Class.create();
UserFilterAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCompanyUserFilter: function() {
var uid = gs.getUserID();
var u = new GlideRecord('sys_user');
if (u.get(uid) && u.getValue('company'))
return 'company=' + u.getValue('company'); // encoded query string
// No company? return a restrictive filter so it doesn't show all
return 'sys_id=javascript:gs.nil("block_all")';
},
type: 'UserFilterAjax'
});
ommon gotchas (and fixes)
You tested with admin: admin has no company → your function returned empty → all users shown. Fix with restrictive fallback as above.
Function returns a GlideRecord, not a string: Advanced ref-qual must return an encoded query string (e.g., company=...), not a record.
Wrong field name: Filter by company on sys_user, not u_company unless you truly customized. Verify the user really has Company set.
Cache: After changing Script Include / dynamic filter options, hard refresh or log out/in to clear cached qualifiers in portal.
Workspace: If using UI Builder forms, ensure the data broker/form component honors ref-qual; most OOB do, but stale page config can interfere—republish the page.
Minimal working no-code example
Dynamic filter option: company = javascript:gs.getUser().getCompanyID()
Variable ref-qual: DYNAMIC: Users in my company
Minimal working code example
Script Include from above
Variable ref-qual: javascript: new UserFilterAjax().getCompanyUserFilter()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Why it doesn’t work
-
Reference qualifiers run on the client when you type in the reference field.
-
The gs object (and gs.getUser()) are only available on the server, not in client scripts.
-
As a result, the system can’t evaluate gs.getUser().getCompanyID() at runtime on the form.
Fix using a dynamic reference qualifier
You can solve this by using a dynamic reference qualifier or a client-callable script include.
Dynamic Filter Option
-
Go to System Definition → Dynamic Filter Options.
-
Create a new one, for example:
Name: Users from my company
Table: sys_user
Filter: Company → is (dynamic) → My Company -
Then, in your reference field’s qualifier, set:
companyDYNAMIC90d1921e5f510100a9ad2572f2b477fe(Or select it from the list.)
This will automatically filter users by the logged-in user’s company.
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Julia Baus if you feel like this answer helped you... !! please mark as accepted and help to close this thread
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
ServiceNow – Reference Qualifier: Show only users from the same Company as the logged‑in user
Problem
You set a reference qualifier to limit a user reference to users in the same Company as the current user using:
javascript:gs.getUser().getCompanyID()
…but the field still lists all users.
Why this happens (common causes)
1) Wrong return type: Advanced ref qualifiers must return an **encoded query string** (e.g., 'company=<sys_id>'), not a raw sys_id.
2) Admin has no Company: If your test user (often 'admin') has no Company set, the qualifier becomes empty and returns **all** users.
3) Catalog variables / Portal / Workspace: Client-side contexts (SP/UI Builder) can’t evaluate server-only gs calls directly. Use a **Dynamic Filter Option** or **client-callable Script Include** instead.
4) List Collector vs Reference: List Collectors have their own qualifier behavior; make sure you set the qualifier on the **variable**, not just the dictionary field.
5) Caching: Old qualifiers can be cached; a hard refresh or re-open is needed after changes.
Three reliable solutions
A) Zero‑code: Dynamic Filter Option (works platform + portal)
1. System Definition → Dynamic Filter Options → New
- Table: sys_user
- Field: company
- Operator: is
- Value: javascript:gs.getUser().getCompanyID()
- Name: Users in my company
2. On the reference field/variable set Reference qualifier = DYNAMIC: Users in my company
B) Advanced qualifier (server) — correct return value
Reference qualifier (Advanced):
javascript:
(function(){
var c = gs.getUser().getCompanyID();
if (!c) return 'sys_id=javascript:gs.nil("block_all")'; // prevent all users if no company
return 'company=' + c;
})();
Notes:
- Must return an encoded query string.
- Add a restrictive fallback when Company is empty.
C) Client‑callable Script Include (works in SP/Workspace)
Script Include (Client callable = true):
var UserFilterAjax = Class.create();
UserFilterAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
byMyCompany: function() {
var c = gs.getUser().getCompanyID();
if (!c) return 'sys_id=javascript:gs.nil("block_all")';
return 'company=' + c; // encoded query string
},
type: 'UserFilterAjax'
});
Reference qualifier (Advanced) on the variable:
javascript:new UserFilterAjax().byMyCompany()
Extra tips / debugging checklist
- Verify the **Company** field on your test user is populated.
- Test the encoded query in a list: open sys_user list, add filter 'company=<sys_id>' — confirm it returns the expected subset.
- For Portal, ensure the Script Include is in the same scope as the variable (or use x_scope.ClassName).
- For List Collector variables:
- Set the qualifier on the **List Collector variable** itself.
- Optionally set attribute `ref_qual_elements=company` if you want the qualifier to refresh when related fields change.
- Turn on debug: add a temporary gs.log in the Script Include to log the computed company and the encoded query.
- Clear cache where needed: try a new incognito window to avoid stale client cache.
Minimal working examples
1) Dynamic: DYNAMIC: Users in my company
2) Advanced:
javascript:(function(){ var c=gs.getUser().getCompanyID(); return c ? 'company='+c : 'sys_id=javascript:gs.nil("block_all")'; })();
Conclusion
- Ensure the qualifier **returns an encoded query**, not just a sys_id.
- Don’t test with an admin that has no Company; add a fallback to avoid “all users”.
- For Portal/Workspace, prefer Dynamic Filter Options or a **client‑callable Script Include**.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
