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

tony_barratt
ServiceNow Employee
ServiceNow Employee

HI Rakesh,



This post indicates the answer is no:


How can I tell what item a sys_id came from?



Best Regards



Tony


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 + '.');


      }


}  


 

This script still works, today (05/2020); thank you.  Can be run from "System Definition - Scripts - Background".  Made a small code style update:

function do_find_sys_id(sys_id) {
	gs.log('Begin search for sys_id: ' + sys_id);

	var table_name = [];
	var table = new GlideRecord('sys_db_object');
	table.orderBy('name');
	table.query();

	while (table.next()) {
		var tableName = table.name.toString();
		
		//If the table is not one that we wish to Exclude...
		if ( (tableName != '')
			&& (! tableName.startsWith('dis'))
			&& (! tableName.startsWith('dsc'))
			&& (! tableName.startsWith('ecc'))
			&& (! tableName.startsWith('ha_'))
			&& (! tableName.startsWith('sh$'))
			&& (! tableName.startsWith('sysx'))
			&& (! tableName.startsWith('ts'))
			&& (! tableName.startsWith('v_'))
			&& (! tableName.startsWith('wf_'))
			&& (! tableName.startsWith('win'))
			&& (! tableName.startsWith('pa_scores'))
			&& (tableName != 'cert_instance')
			&& (tableName != 'u_market_assignment_routing') )
		{

			gs.log('Processing table: ' + table.name); //Used for debugging only.
			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 ' + sys_id + ' in tables: ' + table_name.join(', '));
	}
	else {
		gs.log('Did not find sys_id ' + sys_id + '.');
	}
}

do_find_sys_id("6b18818513ae36001ca459812244b0b7"); //Your sys_id here

G24
Kilo Sage

This Geoffcox script still works, today (05/2020); thank you.  Made a small code style update. Can be run from "System Definition - Scripts - Background".

function do_find_sys_id(sys_id) {
	gs.log('Begin search for sys_id: ' + sys_id);

	var table_name = [];
	var table = new GlideRecord('sys_db_object');
	table.orderBy('name');
	table.query();

	while (table.next()) {
		var tableName = table.name.toString();
		
		//If the table is not one that we wish to Exclude...
		if ( (tableName != '')
			&& (! tableName.startsWith('dis'))
			&& (! tableName.startsWith('dsc'))
			&& (! tableName.startsWith('ecc'))
			&& (! tableName.startsWith('ha_'))
			&& (! tableName.startsWith('sh$'))
			&& (! tableName.startsWith('sysx'))
			&& (! tableName.startsWith('ts'))
			&& (! tableName.startsWith('v_'))
			&& (! tableName.startsWith('wf_'))
			&& (! tableName.startsWith('win'))
			&& (! tableName.startsWith('pa_scores'))
			&& (tableName != 'cert_instance')
			&& (tableName != 'u_market_assignment_routing') )
		{

			gs.log('Processing table: ' + table.name); //Used for debugging only.
			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 ' + sys_id + ' in tables: ' + table_name.join(', '));
	}
	else {
		gs.log('Did not find sys_id ' + sys_id + '.');
	}
}

do_find_sys_id("6b18818513ae36001ca459812244b0b7"); //Your sys_id here