How to identify a record based on sys_id

mamidirakesh
Giga Contributor

Is there a way to identify the record in an instance just based on the sys_id, without knowing the table name?

1 ACCEPTED SOLUTION

geoffcox
Giga Guru

I just wrote and tested the following scheduled job which will look for a table that has a record with a particular sys_id. I exclude some tables from the search.



do_find_sys_id('e836b3e5b0be02008cbdd6bd573aaccf');



function do_find_sys_id(sys_id) {


      var job_name = 'Find sys_id';


      gs.log('========== Begin ' + job_name + ' for ' + sys_id + ' ==========');


      var table_name = [];


      var table = new GlideRecord('sys_db_object');


      table.orderBy('name');


      table.query();


      while (table.next()) {


              if ((table.name.toString() != '') &&


                      (table.name.toString().substring(0,2) != 'ts') &&


                      (table.name.toString().substring(0,2) != 'v_') &&


                      (table.name.toString().substring(0,3) != 'dis') &&


                      (table.name.toString().substring(0,3) != 'dsc') &&


                      (table.name.toString().substring(0,3) != 'ecc') &&


                      (table.name.toString().substring(0,3) != 'ha_') &&


                      (table.name.toString().substring(0,3) != 'wf_') &&


                      (table.name.toString().substring(0,3) != 'win') &&


                      (table.name.toString().substring(0,4) != 'sysx'))   {


                      //gs.log('Processing table ' + table.name + ' (' + table.name.toString().substring(0,3) + ').');


                      var object = new GlideRecord(table.name);


                      if (object.get(sys_id)) {


                              table_name.push(table.name.toString());


                      }


              }


      }              


      if (table_name) {


              gs.log('Found ' + sys_id + ' in ' + table_name.join(', ') + '. Use nav_to.do?uri=' + table_name[0] + '.do?sys_id=' + sys_id);


      }


      else {


              gs.log('Did not find sys_id ' + sys_id + '.');


      }


}  


View solution in original post

5 REPLIES 5

SaschaWildgrube
ServiceNow Employee
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 the earlier proposed solutions.