How to apply dual filters onto a field on a custom Table

Peter Williams
Kilo Sage

Good Morning, 

i have an issue where i am trying to apply dual fitlers onto a reference fields on a custom table.

 

the two reference fields are 

 

Office -> cmn_location table

Department -> cmn_department table

 

the third field i need to filter base on the office + department

 

This third field is a reference to a custom table called u_gl_codes and the columns in this table (Office and Department) is also link to the cmn_location for Office and cmn_department for department

 

I need to be able to filter out the list when users select the office on the table and department to see only the filter of the Gl Codes

 

PeterWilliams_0-1726148606368.png

 

 

PeterWilliams_1-1726148631177.png

 

 

i have tried many options which nothing has worked

 

 

 

1 ACCEPTED SOLUTION

Script Include - 

To implement an advanced reference qualifier using a Script Include in ServiceNow, follow these steps:

  1. Create the Script Include: This Script Include will have a method that queries the records matching the specified office and department fields.

     

    var ScriptIncludeName = Class.create();
    ScriptIncludeName.prototype = {
    initialize: function() {},

    MethodName: function(office, department) {
    var glCodes = [];
    var gr = new GlideRecord('gl_code_table'); // Replace 'gl_code_table' with your actual table name
    gr.addQuery('office', office);
    gr.addQuery('department', department);
    gr.query();

    while (gr.next()) {
    glCodes.push(gr.getValue('sys_id'));
    }

    return glCodes; // Returns an array of sys_ids matching the office and department
    },

    type: 'ScriptIncludeName'
    };

  2. Apply the Reference Qualifier: Use the Script Include in the advanced reference qualifier on the form field where the GL Codes should be filtered:

javascript(Colon) new ScriptIncludeName().MethodName(current.office, current.department);

 

This will dynamically filter GL Codes based on the office and department fields from the current form.

Feel free to let me know if you need any adjustments or clarifications!

 

Thanks,

Sushma.

Mark it as helpful, if it helped you

View solution in original post

34 REPLIES 34

here is my code

var GLFilterOptions = Class.create();
GLFilterOptions.prototype = {
    initialize: function() {},
MethodName: function(office, department) {
var glCodes = [];

gs.info('Script Include invoked');
        gs.info('Office Sys ID: ' + office);
        gs.info('Department Sys ID: ' + department);

var gr = new GlideRecord('u_gl_codes'); // Replace 'gl_code_table' with your actual table name
gr.addQuery('u_office', office); //field names on the gl code table
gr.addQuery('u_department', department); //field names on the gl code table
gr.query();
gs.info('Number of records found: ' + gr.getRowCount());

while (gr.next()) {
glCodes.push(gr.getValue('sys_id'));
gs.info('Found GL Code: ' + gr.getValue('sys_id'));
}
 gs.info('Returning GL Codes: ' + glCodes);
return glCodes; // Returns an array of sys_ids matching the office and department


    },

    type: 'GLFilterOptions'
};

can you send me the screenshot including in the "u_gl_codes" table, office and department backend names

PeterWilliams_0-1726238455966.png

 

PeterWilliams_0-1726238022778.png

PeterWilliams_1-1726238069643.png

 

Bert_c1
Kilo Patron

There is a great OOB example of using a Dependent field. look at 'assigned_to' defined on task that is dependent on 'assignment_group'. The reference qualifier there is "Simple" no script include is needed.