Kostya
Tera Guru

I have often a problem analyzing unfamiliar implementation that I don't know, where a function call or a log trace is placed. Because there may be a "couple" of scripts in ServiceNow you can spend hours studying the source code. Unfortunately, there are no global search function for script code.

I would like to share a background script allow you to get all possible scripts implemented in ServiceNow, custom as well, and than look for a {searchstring}.
It is possible to extend the function with Regular Expression search to searchh more complicated search terms.

Global code search


// Please write here what are you looking for:
var searchstring = 'test';

(function(searchstring) {
// just used for printing results later
var log = new Array();

log.push('Search for character string ' + searchstring + '');

// get all dictionary elements defined with any of script types
var grdict = new GlideRecord('sys_dictionary');
grdict.addQuery('internal_type', 'CONTAINS', 'script');
grdict.addORQuery('internal_type', 'CONTAINS', 'condition');
grdict.addORQuery('internal_type', 'CONTAINS', 'XML');

grdict.addEncodedQuery('nameNOT LIKEvar__m_'); // exclude variable tables
grdict.query();

while(grdict.next()) {
var table = grdict.getValue('name');
var field = grdict.getValue('element');

log.push('Search in ' + table + '.' + field + ' (' + grdict.sys_id + '): ');

// get the records containing the search term in the script field
var gr = new GlideRecord(table);
gr.addQuery(field, 'CONTAINS', searchstring);
gr.query();

while(gr.next()) {
log.push('\tFound in ' + gr.name + '');
}
}

// ... print out
gs.log(log.join('\n'));
})(searchstring);