Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron
Part of the Tips 'N Tricks" series.

 

Other Useful Scripts:

TNT: Useful Script - Find records created/updated within a time window

 

Here's a script that I use from time to time.   It allows me to search for a record using its sys_id.   Sometimes you'll get a message on-screen or in the logs that only mentions the sys_id of a record and you've got not idea what it is.   This script will loop though and look for the sys_id in all the base tables in the system, except for views and some indexing/text search tables.

 

There's an option (addLink = true) to output a message with a link to the record so you can just click on the link and jump to the record.   You will have to copy/paste the link if running as a background script or just click on the link if you are using the Xplore: Developer Toolkit:

 

find_real_file.png

 

When "addLink = false", you'll just get the record details:

 

find_real_file.png

 

The class display label, table name and the record's display value are shown.

 

 

 

 

// Searches for a record using a sys_id

// Replace the "enterYourSys_IdHere" string with the sys_id you are looking for

//

// Optionally add a hyperlink to the record that was found.

// Useful when running the script in the 'Xplore: Developer Toolkit' -   https://share.servicenow.com/app.do#/detailV2/9a1be70e13800b000de935528144b04c/overview

// To add a link, set the "addLink" variable to "true"

 

(function(){

  try {

      //options

      var searchId = "enterYourSys_IdHere";   //the sys_id of the record you are looking for

      //searchId = "4715ab62a9fe1981018c3efb96143495";   //example - an OOB demo Incident

      //searchId = "08fcd0830a0a0b2600079f56b1adb9ae";   //example - an OOB Schedule, '8-5 weekdays'

      //searchId = "62a7bfaf0a0a0a6500c49682bd82376a";   //example - an OOB Business Rule, 'user query'

      var addLink = true; //set to "true" to add a link to the record in the output (Xplore)

 

      //initialize

      var tableName = "";

      var tableLabel = "";

      var tableWeWantToSearch = true;

      var foundRecord = false;

      var message = "";

 

      //loop through all the valid base-class tables (no need to look at any sub-classes)

      var table = new GlideRecord("sys_db_object");

      table.addEncodedQuery("super_class=NULL");

      table.query();

      while (table.next()){

          //get the table name and label

          tableName = (table.getValue("name") + "").toLowerCase();

          tableLabel = (table.getValue("label") + "").toLowerCase();

          tableWeWantToSearch = true;   //assume it is a table we want to search in

 

          //skip views and some other tables that return a lot of probably irrelevant records

          //just comment out the line if you want to include the table in the search

          if (tableName.indexOf("v_") == 0) tableWeWantToSearch = false;                                       //views

          else if (tableName == "ts_c_attachment") tableWeWantToSearch = false;                         //text search indices

          else if (tableName == "ts_chain") tableWeWantToSearch = false;                                       //..

          else if (tableName == "ts_document") tableWeWantToSearch = false;                                 //..

          else if (tableName == "ts_phrase") tableWeWantToSearch = false;                                     //..

          else if (tableName == "ts_word") tableWeWantToSearch = false;                                         //..

          else if (tableName == "ts_word_roots") tableWeWantToSearch = false;                             //..

          else if (tableLabel.indexOf("text index ") == 0) tableWeWantToSearch = false;         //..

          else if (tableLabel.indexOf("ts index stats") == 0) tableWeWantToSearch = false;   //..

          else if (tableLabel.indexOf("recorded incremental change") == 0) tableWeWantToSearch = false;

          else if (tableName.indexOf("sh$") == 0) tableWeWantToSearch = false;

          else if (tableLabel.indexOf("rollback sequence") == 0) tableWeWantToSearch = false;

          else if (tableLabel.indexOf("score level") == 0) tableWeWantToSearch = false;

          else if (tableLabel.indexOf("pa favorites") == 0) tableWeWantToSearch = false;

          else if (tableName.indexOf("syslog") == 0) tableWeWantToSearch = false;      

          else if (tableName.indexOf("sys_cache_flush") == 0) tableWeWantToSearch = false;

          else if (tableName.indexOf("sys_db_cache") == 0) tableWeWantToSearch = false;

 

          if (tableWeWantToSearch){

              var searchTable = new GlideRecord(table.getValue("name"));

              //make sure it is a valid table first

              if (searchTable.isValid()){

                  //searchTable.setWorkflow(false);

                  searchTable.addQuery("sys_id", searchId);

                  searchTable.query();

                  while(searchTable.next()){

                      foundRecord = true;

                      _showFoundRecord();

                  }

              } else {

                  message = "***** Trying to search an invalid table name called '" + table.getValue("name") + "' - the sys_id of that sys_db_object record is '" + table.getValue("sys_id") + "'";

                  gs.addInfoMessage(message);

              }

          }

      }

 

      if (foundRecord == false){

          gs.addInfoMessage("The record was not found");

      }

  } catch(err) {

      gs.log("ERROR: " + err);

  }

 

 

  function _showFoundRecord(){

      var details = searchTable.getDisplayValue();

      if (addLink == true) {

          details = "<a href='" + gs.getProperty('glide.servlet.uri') + "nav_to.do?uri=" + searchTable.getLink() +"' target='_blank'>" + searchTable.getDisplayValue() + "</a>";

      }

      message = "Found a record of type '" + searchTable.getClassDisplayValue() + "' (" + searchTable.getRecordClassName() + ") called '" + details + "'";

      gs.addInfoMessage(message);

  }

})();

 

 

 

 

 

 

 

 

 

I had seen this post a while back by restevao and wanted to post my script but just never got around to it until now.

 

Note: Remember, running scripts as a background script or the Xplore tool can modify data so be aware what you are running.   Always a good idea to test it out on your personal development instance first.

6 Comments