Server-side GlideRecord addQuery case sensitive

xiaix
Tera Guru

Looking at this Case insensitive Glide Record query   I see they talk about client side GlideRecord queries.

I need server side to be case sensitive.

find_real_file.png

The highlighted sections must be a cAsE sEnSiTiVe check.

How can I make it so?

10 REPLIES 10

I learned something new: compareThisString is similar to, and probably better than, "="

MGanon
Tera Guru

GlideRecord server side queries are case insensitive but JavaScript comparisons are case sensitive. I combined the 2 functions to restrict by case sensitivity. I ran a service side GlideRecord query with a while.next to find all case insensitive results. Inside that loop, I compare each result with the query source. If the loop finds a match, immediately return the result.

In my case, I need a transform map to create new records whenever I cannot find a match. If the while loop completes without finding a JavaScript match then I create the record with a GlideRecord insert then return the result. I then set the coalesce field to match case sensitive.


Example:

var matchid = 0;
var rcrd = new GlideRecord('table_name');
      rcrd.addQuery('field',source.u_data);
      rcrd.query();
      while(rcrd.next()){
            if (rcrd.name == source.u_data){
                  matchid = rcrd.sys_id;
                  return matchid;
            }
      }
if (matched == 0) {
      rcrd.initialize();
      rcrd.newRecord();
      rcrd.field = source.u_data;
      matchid = rcrd.insert();
      return matched;
}

Ron28
Kilo Sage

I realise we've moved on a few versions of ServiceNow, I've just tested this in Xanadu after having an issue with an encoded query being case sensitive instead of insensitive as I found SerivceNow is not consistently case insensitive.

Using a script returning an encoded query I did a compare of a string field, where 16 records I read had a "u_service" (string) column containing "ABC". I changed 8 of those to "abc", left the other 8 as "ABC". 

I ran the query first time looking for "abc" in lower case. The result was 16 records. I got the same result of 16 records when doing a search for "ABC", so when comparing a string field I agree the search is indeed case insensitive.

I then changed to compare a choice field, u_phase (I'm using Kubernetes Namespace records here).  I have records in the Kubernetes Namespace table with both upper and lower case system phase.
Run an encoded query on "u_phase=PRD" did not find the records with a system phase of "prd", and in order to select all records I have to add a compare for both lower case and upper case "u_phase=PRD^u_phase=prd". 
So in case of a choice field, ServiceNow is case sensitive.

Just executed the following in my PDI.

var gr = new GlideRecord('sys_user');
// gr.addQuery('name', 'abel tuter');
gr.addEncodedQuery('name=abel Tuter');
gr.query();
while(gr.next()) {
	gs.print(gr.getValue('name'));
}

Result:

Script execution history and recovery available here
*** Script: Abel Tuter

 

 Looking at the result, it seems like case insensitive to me.

I know it works on string fields, but not on a choice list.