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 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
Kilo Guru

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

Correcting my previous reply: I'm seeing all division records in the 'Division' field of the Training Budget form.

@swethavelu 

it should work.

Any extra query BR applied on sys_user other than OOTB one?

💡 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

sangrulkaradvai
Kilo Guru

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()

@sangrulkaradvai 

 

Thank you so much! The solution using an Advanced Reference Qualifier with a Script Include worked perfectly. I was able to filter the Division field based on the logged-in user's role as Division Manager, and allow full access for admins. Appreciate the guidance! 🙌