How to find a Table Name using sys_id?

harshayer
Tera Contributor

I have a sys_id value of a table and I want to find out the table associated with that sys_id. Is there any way i can find out the table or record name using the sys_id?

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Sandeep,



Sample script here. Please adjust it as per your need.


var gr = new GlideRecord('sys_db_object');


gr.addQuery('sys_id','YOURSYSID HERE');


gr.query();


while(gr.next())


  {


  gs.addInfoMessage(gr.name); //This will give you table name


}


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki


You can fire it from Background script for testing purpose. More info here.


Background Scripts — ServiceNow Elite  


View solution in original post

17 REPLIES 17

Chuck Tomasi
Tera Patron

Hi Sandeep,



Not that I can find. You would have to write a script to go through each table in the dictionary and see if your sys_id matches any records in each table. With approximately 2000 tables, this is less than optimal.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Sandeep,



Sample script here. Please adjust it as per your need.


var gr = new GlideRecord('sys_db_object');


gr.addQuery('sys_id','YOURSYSID HERE');


gr.query();


while(gr.next())


  {


  gs.addInfoMessage(gr.name); //This will give you table name


}


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki


You can fire it from Background script for testing purpose. More info here.


Background Scripts — ServiceNow Elite  


Hi Pradeep,



Two issues I have with that script. First, it's missing a gr.query(), second, it didn't work. You are querying a specific table for a specific record. I threw a sys_id from sysevent_email_action and it didn't retrieve the name of the table from sys_db_object. Sorry.


Thanks Chuck. I made edit and updated the script. The script didn't got copied properly


So now I think the question is can I pass the sys_id of the table and retrieve the name..Yes it should work if you pass the sys_id of the record.



Correct me if I am wrong or missing something.