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

restevao
Giga Expert

V2


Looks up the sys_metadata table.


Much faster searching. not sure if absolutely all items are searched



var searchSys_id = "22dbfb7bd7120100f2d224837e6103ff";


 


      var y = new GlideRecord('sys_metadata');


      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 + " Name: " + y.name + " Class: " + y.sys_class_name);


                      gs.log(y.getRowCount());


              }


      }


Replace y.name with y.getDisplayValue()

Should help recognize the record by its identifier rather than say "undefined" if there exists no field called name in the table where the search sys_id record lies.

NRHinton
Tera Contributor

I stumbled across this and have been using it for awhile. It's a simple block of code, but really helpful. Thank you!



Here is my take on a V3 that I use:



var searchSys_id = "77fc3465137bea00360dbe622244b011";


var y = new GlideRecord('sys_metadata');


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.print(" ID: " + y.sys_id + " Name: " + y.name + " Class: " + y.sys_class_name);


  var foundList = y.sys_class_name;


  gs.print(y.getRowCount());


  }


}


var gr = new GlideRecord(y.sys_class_name);


gr.query();


var found = false;


var count = 0;


var foundcount = 0;


while(gr.next()){


  count += 1;


  if (gr.sys_id == searchSys_id){


  gs.print("**************************** Sys_ID Found");


  gs.print("#" + count + "     " + gr.sys_id + "     " + gr.name);


  var foundRecord = gr.name;


  found = true;


  break;


  }


}


if(found == false){


  gs.print("No records found with this sys id");


}


else{


  gs.print("**************************** Table and Record");


  gs.print("Table:   " + foundList);


  gs.print("Record: " + foundRecord);


}


Any chance you can reformat your post so the code appears as code and not a giant text blob?    



Thank you!