- 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
This is possibly as a result of script sandboxing.
Instead of using a javascript expression, have you tried using the 'is dynamic' operator and 'My Company'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
What do you mean with script sandboxing in this case?
For reference qualifier there is no option is(dynamic) when I select Company as the field. It is there for Manager or Division, but not for Company
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
When running scripts via a filter as you have shown, they are run in a limited sandbox which doesn't have access to the same functions/APIs as the rest of the platform. These will silently fail.
If you don't have a dynamic filter option available, you can easily create one via the 'Dynamic filter options' menu item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I created the filter and applied it in the variable and I also check in the background if the function returns a company, 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?
