Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

var searchSys_id = "0eace47cbf1211003f07e2c1ac07396c";




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);


}




I can't get the code working in my instance. It appears the first table query on 'sys_metadata' returns nothing.



Any idea why?


What version of SN are you running?   I tried it as far back as Fuji and it works.


I am using Helsinki.


i have noticed some issues when using V2 or V3 when searching for an incident sys_id. they have worked when looking for other arbitrary sys_id



V1 still works. it just takes longer.