How to 'Does Not Start With' and 'Does Not End With' Operator in GlideRecord query - SOLVED

Filip Vojt__ek
Mega Guru

How to 'Does Not Start With' and 'Does Not End With' Operator in GlideRecord query

 

The ServiceNow seems to not provide this functionality, but there is a way which is not documented.
We can still make use of SQL 'LIKE' and /or  'NOT LIKE' operator. 

 

There is one limitation: All records with empty column where you perform 'NOT LIKE' condition are filtered out too.


Use Cases:

  • Query all User groups [sys_user_group] with Name NOT starting on 'app' (not case sensitive)

 

var userGroupGR = new GlideRecord('sys_user_group');
userGroupGR.addQuery('name', 'NOT LIKE', 'app%');
userGroupGR.query();

gs.print(userGroupGR.getRowCount());

 

Verification: 'Number returned from script' + 'Count of records with Name starting with "app"' + 'Count of records with empty Name' = 'All Records count'

 

 

  • Query all User groups [sys_user_group] with Name NOT ending on 'users' (not case sensitive)

 

var userGroupGR = new GlideRecord('sys_user_group');
userGroupGR.addQuery('name', 'NOT LIKE', '%users');
userGroupGR.query();

gs.print(userGroupGR.getRowCount());

 

Verification: 'Number returned from script' + 'Count of records with Name ending with "users"' + 'Count of records with empty Name' = 'All Records count'

2 REPLIES 2

revathy murugan
Tera Contributor

How to check for combination value.

for example : i need records with "name" column value (does not start with A- and end with -ADMIN)

example "A-servicwnow-Admin "

Hi Revathy,

sorry, completely missed your post.

// Can't start with 'A_' and at the same time can't end with '-ADMIN'
var userGR = new GlideRecord('sys_user');
userGR.addQuery('name', 'NOT LIKE', 'A_%');
userGR.addQuery('name', 'NOT LIKE', '%-ADMIN');
userGR.query();