GlideFilter is Broken - Free Tool: “BetterGlideFilter”
Once upon a time, there was a global, instantiatable “GlideFilter” object. It was a little ugly and unintuitive, but it technically worked; usually. Nowadays though, the GlideFilter object is a scoped-API object (also available to the global scope) that is not instantiatable, and which has… some problems.
ServiceNow is aware of these problems, but - although they continue to use GlideFilter in their scoped apps (such as for vulnerability grouping rules in the Vulnerability application, which have been broken by the use of GlideFilter as of London - PRB1329737) - they have refused to fix the issues (PRB605673).
“What problems”, you ask? - Let’s say you’ve got an Incident with the number “INC000123”. You might have an encoded query that looks something like this:
active=true^number=inc000123
If you use that encoded query to filter the list view on the Incident table, that’ll work just fine.
It’ll also work just fine if you use that query in a script, such as this:
var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery('active=truenumber=inc000123');
grIncident.setLimit(1);
grIncident.query();
if (grIncident.next()) { gs.print('Found INC000123!');
}
But let’s say you’ve already got a GlideRecord (let’s call it current), and you want to compare it to an encoded query, to see if the record matches the query. You can imagine a scenario in which you’ve got a “condition” field on one record, and you want to compare the condition in that field to another record. What then?
That’s what GlideFilter is (supposed to be) for. The idea is to write a single line of code, and get a boolean indicating whether the GlideRecord matches the encoded query, like so:
var doesRecordMatch = GlideFilter.checkRecord(current, 'active=truenumber=inc000123');
However, even if you have an active Incident with the number INC000123, and even if the query works just fine in a scripted query and in the list filter, the above line of code will actually set the doesRecordMatch variable to false.
Why? - Because GlideFilter.checkRecord(), is case-sensitive.
Why again? - I have no idea. It’s not documented that it’s case-sensitive, and I can’t think of a reason why it should be case-sensitive, but it is, and there’s no way to make it not case-sensitive.
Okay, yeah, but what’s the solution
Alright, enough bitching about GlideFilter, here’s the solution. It’s actually fairly simple (which makes it all the more frustrating that they’ve said they won’t fix it, but I digress); simply copy the below script into a Script Include in your instance.
Name: BetterGlideFilter
Accessible from: All application scopes
Application: Global
var BetterGlideFilter = { checkRecord: function(gr, queries, matchAll) { var i, encQuery; var newQueryToken = 'NQ'; var queryMatches = false; matchAll = (typeof matchAll == 'undefined') ? (false) : (matchAll); if (queries.indexOf(newQueryToken) >= 0) { queries = queries.split(newQueryToken); } else { queries = [queries]; } for (i = 0; i < queries.length; i++) { encQuery = queries[i]; queryMatches = this._check(gr, encQuery); if (matchAll && !queryMatches) { return false; } if (!matchAll && queryMatches) { return true; } } return queryMatches; }, _check: function(gr, query) { var grCheck = new GlideRecord(gr.getTableName()); grCheck.addQuery('sys_id', gr.getValue('sys_id')); grCheck.addEncodedQuery(query); grCheck.setLimit(1); grCheck.query(); return grCheck.hasNext();
}
};
With the above Script Include in your instance, you’ve simply got to update any references to “GlideFilter”, to instead reference “BetterGlideFilter”, and you now have a case-agnostic solution.
A brilliant fellow ServiceNow developer, János Szentpáli, reached out to me today, to let me know that he’s found that, despite what HI support has told us, GlideFilter does in fact have an (undocumented) mechanism by which to force it to be case-insensitive: by using it as a constructor!
Of course, one should be wary of becoming heavily reliant on undocumented functionality; but the functionality that he’s demonstrated does have at least one advantage over the tool I’ve written above. It can be used against records which have not yet been committed to the database. Certainly if your use-case relies on that ability, then you’d probably be best-off using the method that János describes in his article!
Check out his excellent article here, for more info.
- March 2024
- February 2024
- Feb 12, 2024 5 Lessons About Programming From Richard Feynman
- July 2023
- May 2023
- April 2023
- December 2022
- Dec 13, 2022 ServiceNow Developers: BE THE GUIDE!
- October 2022
- August 2022
- March 2022
- February 2022
- May 2021
- April 2021
- February 2021
- November 2020
- Nov 17, 2020 SN Guys is now part of Jahnel Group!
- September 2020
- July 2020
- January 2020
- Jan 20, 2020 Getting Help from the ServiceNow Community
- December 2019
- November 2019
- April 2019
- March 2019
- February 2019
- Feb 27, 2019 Making Update Sets Smarter - Free Tool
- November 2018
- October 2018
- September 2018
- July 2018
- Jul 23, 2018 Admin Duty Separation with a Single Account
- June 2018
- May 2018
- May 29, 2018 Learning ServiceNow: Second Edition!
- April 2018
- March 2018
- February 2018
- Feb 11, 2018 We have a new book!
- November 2017
- September 2017
- Sep 12, 2017 Handling TimeZones in ServiceNow (TimeZoneUtil)
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- Mar 12, 2017 reCAPTCHA in ServiceNow CMS/Service Portal
- December 2016
- November 2016
- Nov 10, 2016 Chrome Extension: Load in ServiceNow Frame
- September 2016
- July 2016
- May 2016
- May 17, 2016 What's New in Helsinki?
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- October 2015
- August 2015
- Aug 27, 2015 Easily Clone One User's Access to Another User
https://snprotips.com/blog/2019/3/11/glidefilter-is-broken-heres-an-alternative