Skip to content

Commit

Permalink
Add more tables to code search (ServiceNowDevProgram#858)
Browse files Browse the repository at this point in the history
* Create readme.md

* Create create_admin_user.js

* Create add_more_tables_to_code_search.js

Initial commit

* Create readme.md

initial commit

* Update readme.md

Extended the readme
  • Loading branch information
mskoddow authored Oct 15, 2023
1 parent e2884dc commit dc18551
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var grSearchGroup = new GlideRecord('sn_codesearch_search_group');

grSearchGroup.query();

//iterate all code search groups
while (grSearchGroup.next()) {
var grDictionary = new GlideRecord('sys_dictionary');
var strSearchGroupSysID = grSearchGroup.getValue('sys_id');
var objArrayUtil = new ArrayUtil();

//determine all candidates for a code search from the dictionary
grDictionary.addEncodedQuery(
'internal_type.nameINscript,condition,condition_string,script_plain,XML,script_server' +
'^ORelement=reference_qual' +
'^ORelement=calculation' +
'^NQelementSTARTSWITHscript' +
'^ORelementLIKE_script' +
'^internal_type.nameSTARTSWITHstring' +
'^ORinternal_type.name=json' +
'^NQname=sys_variable_value' +
'^element=value'
);

grDictionary.query();

while (grDictionary.next()) {
var grCodeSearch = new GlideRecord('sn_codesearch_table');
var strTable = grDictionary.getValue('name');
var strField = grDictionary.getValue('element');

//load everything which is already registered as code search base
grCodeSearch.addQuery('table', strTable);
grCodeSearch.addQuery('search_group', strSearchGroupSysID);
grCodeSearch.setLimit(1);
grCodeSearch.query();

//for the respective table there is already a record available
if (grCodeSearch.next()) {
var arrFields = grCodeSearch.getValue('search_fields').split(',');

arrFields.push(strField);
grCodeSearch.setValue('search_fields', objArrayUtil.unique(arrFields).join(','));
grCodeSearch.update();
}
// create a new record at table "sn_codesearch_table"
else {
grCodeSearch.initialize();
grCodeSearch.setValue('table', strTable);
grCodeSearch.setValue('search_group', strSearchGroupSysID);
grCodeSearch.setValue('search_fields', strField);
grCodeSearch.insert();
}
}
}
19 changes: 19 additions & 0 deletions Background Scripts/Extend Code Search Base./readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Description
Over time, your code spreads across dozens of artifact types and it becomes very difficult to keep track of it all.

The whole thing is made more difficult by the fact that JavaScript code is not always contained in pure script fields but also, for example, in conditions or even in JSON payloads.

Fortunately, there is a code search - for example in the ServiceNow Studio, via the UI page "CodeSearchExampleUse" or with the help of the browser extension "SN Utils".

However in a baseline instance the code search covers about 31 tables and their fields only. If you want to search all available tables and script-related fields, you can execute my script `add_more_tables_to_code_search`. It determines in the dictionary all potential candidates that could hold JavaScript code and add them to the code search base.

# Involved tables

## `sn_codesearch_search_group`

A search group has a certain name and covers all tables and fields which can be searched when using that search group. Utilizing "SN Utils" browser extension you can select the preferred search group. Within ServiceNow Studio this is not possible and the search group "sn_devstudio.Studio" is leveraged automatically. For this reason my script adds all tables and fields to all search groups the same way.

## `sn_codesearch_table`

Each record in that table represent a table and their fields which are searchable. These records are added or complemented by my script.

0 comments on commit dc18551

Please sign in to comment.