Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Search for a specific sys_id

restevao
Giga Expert

Search for a sys_id in your entire serviceNow instance.

Some time your scripts/logs may spit out a sys_id that you cant identify without spending some time to manually search for it.

This script will automatically search for the sys_id and return more data to identify the record.

The time taken for this will vary depending on the size of the instance. Off my dev instance, it took about 2 mins.

Works by searching all the tables via the "sys_db_object", then applying that table name to another query, which then searches for a second time.

The results are spit out in script logs

Recommended for a developer who knows what they are doing, but is currently safe as is. **use/modify at your own risk**

Code to delete the found record is commented out at the top. Untested and possibly dangerous.

Install by creating a fix script and change the 'searchSys_id' variable at the top of the script. Then run.

You need appropriate roles to run the fix script. - admin.

Please post any enhancement you may make.

FIX SCRIPT:

Script

//Delete multiple records according to the current "where" clause. Does not delete attachments.

//y.deleteMultiple()

//boolean y.deleteRecord()

var searchSys_id = "e6303e0b4fb34200501030318110c7b7";

var x = new GlideRecord('sys_db_object'); //sys_db_object

  1. x.addActiveQuery();
  2. x.query();

//gs.log(x.getRowCount());

while(x.next())

{

      //gs.log(x.name);

      if(x.sys_id == searchSys_id ) // Checks tables own sysID

      gs.log(" ID: " + x.sys_id + "Table Name: " + x.name + " Class: " + x.sys_class_name);

     

      try

      {

              var y = new GlideRecord(x.name);

              y.addQuery('sys_id',searchSys_id);

              y.addActiveQuery();

              y.query();

              while(y.next())

              {

                      if(y.sys_id == searchSys_id ) // NEED this double check or else log will spit out alot lot of rubbish

                      {

                              gs.log(" ID: " + y.sys_id + " Table Name: " + x.name + " Name: " + y.name + " Class: " + y.sys_class_name);

                              gs.log(y.getRowCount());

                      }

              }

      }

      catch(e)

      {

          gs.log("ID ERROR: " + e);

      }

}

14 REPLIES 14

Daniel Cudney
Kilo Contributor

var SYSIDSearch = "774190f01f1310005a3637b8ec8b70ef";  



var complete = false;



var gr = new GlideRecord('sys_db_object');



if (gr.get(SYSIDSearch)) {


        gs.log(gr.name, 'SYSIDSearch');


        complete = true;


}


else {



gr = new GlideRecord('sys_db_object');



gr.addActiveQuery();  


gr.query();



}


while(gr.next() && !complete) {


        try {


                  var grB = new GlideRecord(gr.name);  


                  if (grB.get(SYSIDSearch)) {


                            gs.log(gr.name, 'SYSIDSearch');


                            complete = true;


                  }


        }


        catch(e) {


                  //who cares


        }


}  


Jim Coyne
Kilo Patron

Here's the script I use when searching for a sys_id - Useful Background Script - Find a record by its sys_id


And another for when I need to find records created/updated within a certain time window - Useful Background Script - Find records created/updated within a time window


restevao
Giga Expert

Another enhanced searching tool

Universal Script Searcher

saschawildg
ServiceNow Employee

From time to time the solution needs a face-lift.

New tables appear in the platform and additional exceptions should be added.

The DevTools app contains powerful tools and a code library for ServiceNow developers.

One of them is the GetTableFromSysId() function. Check it out here:

https://github.com/saschawildgrube/servicenow-devtools/blob/master/update/sys_script_include_fc26194...

It excludes a number of tables from the search and searches a number of tables first, where I believe the likelihood of a match is higher - which makes it faster in many cases than many other proposed solutions.