From dc185510084de929e7c13ccacb7bc39b058dd530 Mon Sep 17 00:00:00 2001 From: mskoddow <58812581+mskoddow@users.noreply.github.com> Date: Sun, 15 Oct 2023 19:16:31 +0200 Subject: [PATCH] Add more tables to code search (#858) * 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 --- .../add_more_tables_to_code_search.js | 54 +++++++++++++++++++ .../Extend Code Search Base./readme.md | 19 +++++++ 2 files changed, 73 insertions(+) create mode 100644 Background Scripts/Extend Code Search Base./add_more_tables_to_code_search.js create mode 100644 Background Scripts/Extend Code Search Base./readme.md diff --git a/Background Scripts/Extend Code Search Base./add_more_tables_to_code_search.js b/Background Scripts/Extend Code Search Base./add_more_tables_to_code_search.js new file mode 100644 index 0000000000..d78c72dbd3 --- /dev/null +++ b/Background Scripts/Extend Code Search Base./add_more_tables_to_code_search.js @@ -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(); + } + } +} diff --git a/Background Scripts/Extend Code Search Base./readme.md b/Background Scripts/Extend Code Search Base./readme.md new file mode 100644 index 0000000000..3ec06a48bd --- /dev/null +++ b/Background Scripts/Extend Code Search Base./readme.md @@ -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. +