how to find if a script contains sub string

learnSN
Tera Expert

if we have two  sub strings in string (GlideRecord ,Alert), how check the two sub strings 

please write a program to check whether a string contains sub string

9 REPLIES 9

Hi,

so you want to check

1) if client script contains alert or GlideRecord and add to your custom table with the type and the name of the script which contains that

try this sample script include functions which you can call

GlideRecord checking

checkClientScriptForGlideRecord: function(){

var rec = new GlideRecord('sys_script_client');

rec.addEncodedQuery('sys_class_name=sys_script_client^scriptLIKEnew GlideRecord(');

rec.query();

while(rec.next()){

var customRec = new GlideRecord('your custom table');

customRec.initialize();

customRec.u_type = 'Client Script';

customRec.u_substring = 'GlideRecord(';

customRec.u_name = rec.name;

customRec.insert();

}

},

Alert checking

checkClientScriptForGlideRecord: function(){

var rec = new GlideRecord('sys_script_client');

rec.addEncodedQuery('sys_class_name=sys_script_client^scriptLIKEalert(');

rec.query();

while(rec.next()){

var customRec = new GlideRecord('your custom table');

customRec.initialize();

customRec.u_type = 'Client Script';

customRec.u_substring = 'alert(';

customRec.u_name = rec.name;

customRec.insert();

}

},

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello there!

i have tried this with script include ,result is it is showing the violations with their respective sys_id also but here is the problem is alerts function table have different description and recommendation and GlideRecord have different too but both records contains GlideRecord description and recommendation.

please find the attachment so that you can 

> first sys id contains alerts function

> last two  sys id's contains Gliderecords 

NOTE: please make sure code should be in dynamic way

thank you

Hi Naresh,

I believe the script which was required as per your question has been shared.

You will have to enhance it as per your requirement.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Naresh,

Any update on this thread?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hitoshi Ozawa
Giga Sage
Giga Sage

Not sure of the question.

Following code will find all records in Alerts table (em_event) that has a particular string.

var stringToSearch = '<string to search>';

var grAlert = new GlideRecord('em_event');
grAlert.addEncodedQuery('<query string>');
grAlert.query();
while (grAlert.next()) {
  gs.info(grAlert.sys_id);
}

Query string can be obtained by opening Alert table and setting filter conditions and running it. Right click on the filter breadcrumb and select "Copy query". Paste the query to the script above. "contains" filter can be used to find occurrence of a string in field.

find_real_file.png