Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Reference qualifier for company is not working

Julia Baus
Kilo Guru

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?

 

1 ACCEPTED SOLUTION

MaxMixali
Giga Guru

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&colon;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&colon;
(function(){
var c = gs.getUser().getCompanyID();
if (!c) return 'sys_id=javascript&colon;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&colon;gs.nil("block_all")';
return 'company=' + c; // encoded query string
},
type: 'UserFilterAjax'
});

Reference qualifier (Advanced) on the variable:
javascript&colon;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&colon;(function(){ var c=gs.getUser().getCompanyID(); return c ? 'company='+c : 'sys_id=javascript&colon;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**.

View solution in original post

15 REPLIES 15

Anand2799
Tera Guru

Hi @Julia Baus ,

 

Try this:

Anand2799_0-1761234373369.png

 

Try logout and login if gs.getUser().getCompanyID() is returning empty value (Verify in background script).

 

Thanks

Anand

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?

Nawal Singh
Tera Guru

Hi @Julia Baus ,

I have tested the below code in its working fine in PDI, Please review -

 

Reference Qualifier-  " javascript&colon; new LoggedInUserUtils().getCompanyUser(gs.getUserID()); "

 

NawalSingh_0-1761238129487.png

 

and Script Include- 

 

   getCompanyUser: function(userID) {

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', userID);
        gr.query();
        if (gr.next()) {
            return 'company=' + gr.getValue('company');
        }

    },
 
NawalSingh_1-1761238248909.png

 

If you found my response helpful, please mark it as helpful and accept it as the solution for Future Readers

Thank you
Nawal Singh

MaxMixali
Giga Guru

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&colon; 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&colon; 'company=' + gs.getUser().getCompanyID()

Optional (to show only active users):
javascript&colon; '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&colon; 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&colon; '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&colon; 'company=' + gs.getUser().getCompanyID()

This reliably filters users to those whose company matches the logged-in user.

I tried with the different javascript and also checked for value in the background script, but still the same result: I get all users returned (even when doing it as an admin...)