matches regex or matches pattern

stevenm
Kilo Guru

So I have a requirement to filter SN on the 5th and 6th positions of a server name. This is in Helsinki and on a reference field within a Record Producer.   I can't seem to get either matches pattern or matches regex to work.   I get a lot of SQL/MySql errors.   What's the trick?   I'll keep googling.   Thanks.

1 ACCEPTED SOLUTION

Dave Smith1
ServiceNow Employee
ServiceNow Employee

The biggest WTF is that a feature - known broken and recommended not to be used - still hasn't been pulled.

 

If it doesn't work and serves no purpose... why leave it in?

View solution in original post

16 REPLIES 16

nayanawadhiya1
Kilo Sage

Hello Steve,



Take the Display Value of Reference Field (Server Name) like this -


g_form.getDisplayBox('field_name ').value;



After that perform your matches regex-


https://www.w3schools.com/jsref/jsref_match.asp


stevenm
Kilo Guru

Why do I have matches pattern and matches regex available if I can't use them?   I've tried with quote without quote with single quotes etc.



MatchesPattern.JPG



Error.JPG


I agree Steve...I can't get this to work.



Can we get an expert to chime in on how to use these pattern match options?


I have no idea how to get the items to work that I tried to work.   So I went with a script includes in the reference qualifier field of a list collector instead.   HOWEVER, it returns every record not my filtered records.   Here is my code.   It's been tweaked from code I found elsewhere.   Not sure why I get every record returned though.




var KHNCheckTrackboardComputers = Class.create();


KHNCheckTrackboardComputers.prototype = {


checkcomputers: function() {


var str = "";


var regexp = /^.{4}(sn|SN|Sn|sN)/g;


var gp = "";


var firsttime = true;



//Get a query of computers with SN in their name.


var grp = new GlideRecord('cmdb_ci_computer');


grp.addQuery('name', 'CONTAINS', 'SN');


grp.query();



gs.log('smsb debug rowcount -> ' + grp.getRowCount());



while(grp.next()) {


str = grp.name;


//gs.log('smsb debug value of str -> ' + str);


if (regexp.test(str))


{


gs.log('smsb debug found a regex match');


if (firsttime)


{


gp += grp.name;


firsttime = false;


}


else


{


//build a comma separated string of groups if there is more than one


gp += (',' + grp.name);


}


}


}


gs.log('smsb debug value of gp -> ' + gp);


return 'sys_id_IN' + gp;


},



type: 'KHNCheckTrackboardComputers'


};