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.

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

G24
Kilo Sage

Thanks @Yerapothini Rak That was extremely helpful. 

 

I posted a different version here which is intended to be a little easier to edit and see results for.  Included screenshots.  Hyperlinks are immediately clickable when run from Script Background.  I'm sure there are many versions floating around.  Thanks again.