- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 06:44 AM
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
i have tried many options which nothing has worked
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 07:18 AM - edited 09-12-2024 07:22 AM
Script Include -
To implement an advanced reference qualifier using a Script Include in ServiceNow, follow these steps:
- 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'
}; - 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 10:34 AM
If I understand correctly, you have a new custom table for GL codes that has 2 reference fields for office and deparment. There is another table that has the same two reference fields plus a reference field to the GL codes table. If so, I duplicated that in my PDI.
the script include is named 'officeDeptCodes' and follows:
function officeDeptCodes(office, dept) {
var codes = new GlideRecord('u_gl_codes');
var result = [];
codes.addQuery('u_office', office);
codes.addQuery('u_department', dept);
codes.query();
// gs.info('officeDeptCodes: Found ' + codes.getRowCount() + ' records');
while(codes.next()) {
result.push(codes.sys_id.toString());
}
// gs.info('officeDeptCodes: returning: ' + result);
return result;
}
my table that has a reference field to the GL codes table has the field defined as:
javascript:'sys_idIN'+officeDeptCodes(current.u_office, current.u_department);
As I said earlier you need to be specific on what you want. Change field and table names to match what you have.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 11:17 PM
Is it not working now with the given script? Please let me know if you changed the name of the table.
As you mentioned, depending on the department, can you specify where is the exact issue?
Thanks,
Sushma.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 05:16 AM
ya its not working, i change the table name and adjusted the current.office and current.department names to the correct ones and still its not filting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 05:18 AM
Can you elaborate and add some screenshots where you are facing the issue?
Thanks,
Sushma.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 06:12 AM