- 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
2 weeks ago
HI @Julia Baus,
please consider revoking their answer as @MaxMixali is posting tens of messages with the same structure and they just copy paste if from some gen AI tool, look at their replies - they psot 40 replies in an hour. It's clear that it's cheating and in collision with ServiceNow Community Code of Conduct for all members
You said they were closest, but in fact they couldn't be more far... that's just FYI. Use this piece of information as you think it's fair :)))
Have a wonderful day
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
