Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Reference Qualifier Not Working in Scoped App for Filtering Division Field Based on Logged-in User

swethavelu
Tera Contributor

Hi Community,

 

I’m working in a custom scoped application and need help with a reference qualifier issue.

Requirement:

I have a Training Budget table with a reference field called division, which points to a division table which is extended from Department table. The Division table contains fields like:

  • name
  • dept_head (reference to sys_user, representing the Division Manager)

When a user opens the Training Budget form, I want the Division field to show only those divisions where the logged-in user is the Division Manager (dept_head).
However, if the user has the admin role, they should see all divisions.

 

What I Tried:

I used the following onLoad client script:

 
function onLoad() {
if (!g_user.hasRole('admin')) {
g_form.addInfoMessage("User is NOT an admin. Applying reference qualifier.");
var userId = g_user.userID;
g_form.setReferenceQual('division', 'dept_head=' + userId);
} else {
g_form.addInfoMessage("User IS an admin. No filter applied.");
g_form.setReferenceQual('division', '');
}
}
 

But this is not working — the user still sees all division records when clicking the lens icon. Also, I noticed that g_form.setReferenceQual() throws an error in the console:
TypeError: g_form.setReferenceQual is not a function.

 

Additional Info:

  • The app is in a custom scope.
  • The division field is a reference to a scoped table.
  • I tried setting the Reference Qualifier to Advanced in the dictionary, but it resets to Simple after saving.

Question:

What is the correct way to apply a dynamic reference qualifier in a scoped app so that:

  • Non-admin users only see divisions where they are the dept_head
  • Admins see all divisions

Any working solution or workaround would be greatly appreciated!

Thanks in advance!

1 ACCEPTED SOLUTION

sangrulkaradvai
Tera Expert

ServiceNow best practice for dynamic filtering is to use an Advanced Reference Qualifier with a Script Include. You create a Script Include that returns an encoded query and then call that Script Include function inside the field’s reference qualifier using the javascript: prefix.

Example setup:

  1. Create a Script Include in your scoped application.
    Name: x_training_budget_FilterDivisions , create funcation getDivisionsForUser add logic here 

  2. In the Advanced Reference Qualifier script field, enter:

    javascript: new x_training_budget_FilterDivisions().getDivisionsForUser()

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

@swethavelu 

you cannot apply ref qualifier using client script.

So that approach is wrong.

Simply use this in advanced ref qualifier of that field

Note: Community adds : so replace it with colon character

javascript: var query = '';
if (!gs.hasRole('admin'))
    query = 'dept_head=' + gs.getUserID();
query;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@swethavelu 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

Steps I Followed:

  1. I opened the dictionary entry for the Division field in the Training Budget table, where I want to display the division of the currently logged-in user.

  2. I clicked the Advanced View related link, selected Advanced in the Use Reference Qualifier field, added the code you provided, and saved the changes.

Issue Observed:

After saving, the Use Reference Qualifier field resets to the default value Simple, instead of retaining Advanced.

 

Could you please let me know if I missed any steps or did something incorrectly?

 

Screenshot fyr,

 

swethavelu_0-1761734524830.png

 

@swethavelu 

steps are correct.

Try to save from List instead of Form

what's the outcome with admin and non-admin?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader