- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:12 PM
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();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:57 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:40 PM
Hi, what is the resulting value and data type for var reg?
'addQuery' requires a string input, and as you have it defined without any operator the query will only return results that are an exact match to 'reg'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:46 PM
The data is a string type.
Try the value as "\ D {3}".
The value will be changed dynamically by the user.
I want to filter the message part of a record with a regular expression.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 06:57 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 07:06 PM
It seems that it can be solved.
Thank you very much