SlightlyLoony
Tera Contributor

find_real_file.pngConsider the following code snippet. What do you think it will log (assuming that the query succeeded)?


var gr = new GlideRecord('core_company');
gr.addQuery('name', 'Lenovo');
gr.query();
if (gr.next())
gs.log('Found Lenovo: ' + ('Lenovo' == gr.name));


If you said "True, of course!" you'd be wrong. The right answer is "True, sometimes." Specifically, it will be true if and only if Lenovo was stored in the database with exactly the same combination of upper and lower case characters that you're testing for. But it could be stored as something else, and the query would still succeed. For example, when the record in the database was inserted (or last updated), the company name might have been stored as 'LENOVO' — and in this case, the snippet above would still succeed in the query (because the database is case-insensitive) but fail in the log statement.

How would you work around this issue? By using the case-leveling methods, as below:

var gr = new GlideRecord('core_company');
gr.addQuery('name', 'Lenovo'.toLowerCase());
gr.query();
if (gr.next())
gs.log('Found Lenovo: ' + ('Lenovo'.toLowerCase() == gr.name.toLowerCase()));