Need Script Examples for Dynamic Filter Options and Advanced Reference Qualifier

suryatejak9
Kilo Contributor

Hi ServiceNow Community,

I'm practicing Reference Qualifiers in my ServiceNow PDI.

I have created a custom table called Employee Details with the following fields:

  • u_assignment_group – Reference to Group (sys_user_group)

  • u_assigned_to – Reference to User (sys_user)

I have also created u_department as a Reference field in the Group table.

Requirement:

When I select an Assignment Group (e.g., Database), the Assigned To field should display only users whose Department matches the selected Assignment Group's Department.

For example, if the Database group's Department is IT, Assigned To should show only users belonging to the IT Department.

I have tried using an Advanced Reference Qualifier and a Script Include, but the filtering is not working as expected. However, filtering with a hardcoded Department sys_id works.

Could someone please help me with the scripts for both approaches?

  1. Dynamic Reference Qualifier: How can I write the script in Dynamic Filter Options to achieve this requirement?

  2. Advanced Reference Qualifier: What script should I use in the Advanced Reference Qualifier, with or without a Script Include?

I would appreciate working script examples and the configuration steps for both approaches so I can understand the difference.

Thanks in advance!

2 REPLIES 2

Rohit  Singh
Mega Sage

Hi @suryatejak9 

 

You can refer this video for advanced reference qualifier. 

 

https://www.youtube.com/watch?v=lqsaTP64UTE

 

If my response helped please mark it "helpful" and "accept the solution" so that it benefits future readers as well.

 

Regards,

Rohit

 

Reach out us on +91 6304422358 for complete training!! Explore SERVICENOW UDEMY COURSES -- YOUR RATING IS TRULY APPRECIATED!! SERVICENOW SCRIPTING COURSE: https://www.udemy.com/course/servicenow-scripting-mastery-advanced-development/ SERVICENOW ADMIN COURSE: ...

HasanM
Kilo Sage

Hi @suryatejak9 

Both approaches work. Here are minimal versions.

1. Advanced Reference Qualifier (no Script Include needed)

On the dictionary entry for u_assigned_to, set Use reference qualifier = Advanced:

javascript:'department=' + current.u_assignment_group.u_department;

With a Script Include (Name: DeptUserFilter):

var DeptUserFilter = Class.create();
DeptUserFilter.prototype = {
    initialize: function() {},
    byGroupDept: function(groupId) {
        var gr = new GlideRecord('sys_user_group');
        if (gr.get(groupId) && !gr.u_department.nil())
            return 'department=' + gr.getValue('u_department');
        return 'sys_id=-1'; // no group selected, so show no users
    },
    type: 'DeptUserFilter'
};

Reference qualifier:

javascript:new DeptUserFilter().byGroupDept(current.u_assignment_group);

2. Dynamic Filter Option

  1. Add this method to the same Script Include, and tick Client callable. Dynamic filter scripts need it.
usersByGroupDept: function(groupId) {
    var ids = [];
    var grp = new GlideRecord('sys_user_group');
    if (!grp.get(groupId)) return ids;
    var u = new GlideRecord('sys_user');
    u.addQuery('department', grp.getValue('u_department'));
    u.query();
    while (u.next()) ids.push(u.getUniqueValue());
    return ids;
},
  1. Go to System Definition > Dynamic Filter Options and click New:
    • Label: Users in group's department
    • Script: new DeptUserFilter().usersByGroupDept(current.u_assignment_group)
    • Field type: Reference
    • Referenced table: User [sys_user]
    • Available for ref qual: ticked
  2. On the u_assigned_to dictionary entry, set Use reference qualifier = Dynamic and pick that option.

The usual reasons it "doesn't work" are passing the group instead of its department, or a missing javascript: prefix. A Script Include that isn't client callable also stops the dynamic option from working.

 

If this helps, please mark it as helpful.