How to find record name and table name with sys_id of any instance table's record?

Kingstan M
Kilo Sage

Hi SNC,

Greetings.

 

How to find record name and table name with sys_id of any instance table's record?

(i.e.,)

1. I have a sys_id of client script or b.rule or any system related = Yes i can find that in sys_metadata table.

 

Let's take this scenario :

2. I have a sys_id of an Incident record which i do not know that belongs to Incident table. So, i am spending time in searching in RITM table. So, Is there a global tbl where i can find system record in that table?

 

Note

1. I do search in sys_db_object also. But no expected results.

2. I know i can use util., extension to do this. But how that ext., even accomplish this? It should hit a tbl on first hand to search the sys_id under what ever complex logic underlying.

 

Many thanks.

 

1 ACCEPTED SOLUTION

Yerapothini Rak
ServiceNow Employee
ServiceNow Employee

Hi @Kingstan M 

 

You can run the below script in the background scripts in your instance to search the record using the sys_id. Please replace the sys_id with your sys_id and make sure you are in the global domain.

 

(function() {

var baseTables = new GlideRecord('sys_db_object'),
// Change the value below to the sys_id you seek
sys_id = '12c05f90fc34250035dab8143c16e6d2',
current,
dict;

gs.print('Searching base tables in sys_db_object for ' + sys_id)

// grab base tables, excluding text-indexing, virtual, and sysx_ tables, which don't have sys_id
baseTables.addEncodedQuery('super_classISEMPTY^nameNOT LIKEts_c_^nameNOT LIKEsysx_^nameNOT LIKEv_');
baseTables.query();

while( baseTables.next() ) {
gs.print('Searching: ' + baseTables.name);
current = new GlideRecord( baseTables.name );
dict = new GlideRecord( 'sys_dictionary' );

// GlideRecord.isValidField does not work for sys_id, so we have to look for a sys_id field for the table
dict.addQuery('name', baseTables.name );
dict.addQuery('element', 'sys_id');
dict.queryNoDomain();
if(!dict.next()) continue;

// if( !current.isValidField('sys_id') ) continue; // This is currently broken due to PRB but won't hurt anything
current.addQuery('sys_id', sys_id);
current.setWorkflow(false); // query rules can cull results
current.queryNoDomain();

if( current._next() ) {
gs.print('Table: ' + current.getClassDisplayValue());
gs.print('<a href="' + current.getLink() + '">Link</a>');
break;
}
}

gs.print('Done');

})();

 

Feel free to reply here, if you have any queries.


If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

Thanks

View solution in original post

5 REPLIES 5

Yerapothini Rak
ServiceNow Employee
ServiceNow Employee

Hi @Kingstan M 

 

You can run the below script in the background scripts in your instance to search the record using the sys_id. Please replace the sys_id with your sys_id and make sure you are in the global domain.

 

(function() {

var baseTables = new GlideRecord('sys_db_object'),
// Change the value below to the sys_id you seek
sys_id = '12c05f90fc34250035dab8143c16e6d2',
current,
dict;

gs.print('Searching base tables in sys_db_object for ' + sys_id)

// grab base tables, excluding text-indexing, virtual, and sysx_ tables, which don't have sys_id
baseTables.addEncodedQuery('super_classISEMPTY^nameNOT LIKEts_c_^nameNOT LIKEsysx_^nameNOT LIKEv_');
baseTables.query();

while( baseTables.next() ) {
gs.print('Searching: ' + baseTables.name);
current = new GlideRecord( baseTables.name );
dict = new GlideRecord( 'sys_dictionary' );

// GlideRecord.isValidField does not work for sys_id, so we have to look for a sys_id field for the table
dict.addQuery('name', baseTables.name );
dict.addQuery('element', 'sys_id');
dict.queryNoDomain();
if(!dict.next()) continue;

// if( !current.isValidField('sys_id') ) continue; // This is currently broken due to PRB but won't hurt anything
current.addQuery('sys_id', sys_id);
current.setWorkflow(false); // query rules can cull results
current.queryNoDomain();

if( current._next() ) {
gs.print('Table: ' + current.getClassDisplayValue());
gs.print('<a href="' + current.getLink() + '">Link</a>');
break;
}
}

gs.print('Done');

})();

 

Feel free to reply here, if you have any queries.


If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

Thanks

Hi, Thanks for your response.

But i can not get any o.p here.

I randomly took a INC and copied sys_id and replaced at ln5.

Guess what, I get no INC# in o.p.

 

Could you please test this for instance?

 

Many thanks.

You need to replace the sys_id in line 4

Screenshot 2022-12-20 at 3.22.00 PM.png

Below is the sample output. Please check the incident number as below:

*** Script: Record contents:
sys_meta:sys_meta
made_sla:true
upon_reject:cancel
sys_updated_on:2022-11-28 11:23:40
task_effective_number:INC0019999
number:INC0019999

Ok, this is interesting!

I adjusted log to this and i got the expected.

 

Thanks for letting me know.

 

gs.info('log_00 --Table Name-- : ' + current.getClassDisplayValue());
gs.info('log_00 --Display Value-- : ' + current.getDisplayValue());

 

Note

>> If we can search sys_db_object with INC sys_id then it returns null - Why?

 

Many thanks!