Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Case insensitive Glide Record query

Katie A
Mega Guru

Is there a way to make a glidequery to be case INSENSITIVE?

In other words, I want to check if a record already exists based on a user input string.

I want to match regardless of case.

For example, if the computer name ac123-123 already exists in the database, I want to match the query regardless of whether the user types in AC123-123 or aC123-123 or Ac123-123.

Here is the Catalog Client Script onSubmit.

This will only match if the case is exact. I want to cover all possible case combinations.

                      var cmp_name = g_form.getValue('u_name');

                      var cmpCI = new GlideRecord('cmdb_ci');

                      cmpCI.addQuery('name',cmp_name);

                      cmpCI.query();

                      if (cmpCI.next()){

                                              g_form.showFieldMsg('u_name','Computer name already exists! Unable to create computer.','error');

                                              return false;

                      }

1 ACCEPTED SOLUTION

Hmm, I just tried a client side GlideRecord in my dev instance. I threw this together and it found three Johns when I queried for lowercase.


function onSubmit() {


        var hi = '';


        var gr = new GlideRecord('sys_user');


        gr.addQuery('first_name', 'john');


        gr.query();


        while(gr.next()){


                  hi += gr.name.toString();


        }


        alert(hi);


}



Result:


find_real_file.png


View solution in original post

5 REPLIES 5

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;
}