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

arshanapalli
Tera Contributor

Hi All,



I have tried using the script mentioned above but I did not get the table name after running it in background script:



Here is my script:



var gr = new GlideRecord('sys_db_object');


gr.addQuery('sys_id','b5173b1d0f10860047e27d4ce1050eed');  


gr.query();


while(gr.next())


{


  gs.print(gr.name);


}



I just replaced addInfoMessage with print.


find_real_file.png




Any suggestions would be helpful thanks.


Hello harsha


Please change gs.print to gs.info


Updated Script:


var gr = new GlideRecord('sys_db_object');


gr.addQuery('sys_id','b5173b1d0f10860047e27d4ce1050eed');


gr.query();


while(gr.next())


{


  gs.info(gr.name);


}



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Pete Key
Tera Expert

Hi harshayer,

  It seems there was a lot of confusion regarding your question.  I've run into a similar issue where I'm looking for the table that contains a certain sys_id, for a host of reasons.  The reply from Pradeep contains a simple query that rolls through the sys_db_object table which contains a list of all the tables, regardless of scope.  The part he missed is to add a 2nd nested sub query that will roll through each table, one at a time, and look for your specified sys_id, returning the table name if/when it's found.  As noted by another reply, that can be very time and resource intensive so you might consider creating a Fix Script with this script to run in the background and simply update/add a row to a table if/when a table name is found.  Hope this helps!