I'm looking for a way to use regular expressions in [addQuery]

__17
Tera Expert

After looking at some articles, I thought I could use regular expressions to filter records in the following ways:

However, the expected result was not obtained.
Is there a way to filter records using regular expressions?

The target field is a string type.

 

 

var tmp_rec = new GlideRecord("table_name");
tmp_rec.query();
tmp_rec.next();


var reg = new RegExp(tmp_rec.getDisplayValue("Regular_expression_keywords"));//Regular expression string is set in "Regular_expression_keywords"


var target_rec = new GlideRecord("target_table_name");

target_rec.addQuery(message_field,reg);//"Message_field" is a string type
target_rec.query();

var rec_count = 0; 
rec_count = target_rec.getRowCount();
1 ACCEPTED SOLUTION

If you are trying to use a regex dynamically in a GlideRecord query, then I do not think this is possible unless you use GlideFilter within a 'while.next()' loop.

GlideFilter and Regular Expression Match - Developer Community - Blog - ServiceNow Community

If you are trying to use 'contains' in a Gliderecord query, then you need to use the syntax
addQuery('field_name', 'CONTAINS', 'yourValue');

The link I provided in my first reply contains the operators available and syntax

View solution in original post

6 REPLIES 6

Hi Tony, is this option more performant than just retrieving all the records and going through them one by one?

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi 盆栽,

Correct answer has already been selected but for your information, it's possible to use regular expression in filter in GlideQuery.

e.g. from ServiceNow document.

var filteredQuery = new global.GlideQuery('sys_user')
    .getBy({ sys_id: 'f682abf03710200044e0bfc8bcbe5d38' }, ['phone'])
    .filter(function (user) {
        return phoneRegex.test(user.phone);
    });

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/no-namespace/OptionalGlobalA...