How do we find out the record, given the sys_id?

upasanamahanta
Mega Contributor

Given a sys_id, how do we find to which record the sys_id belongs to?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

You COULD write a script that goes through sys_db_object and checks each table to see if it can find the record. With over 2,000 tables in a default Helsinki instance, this could take a while. The *untested* script might look something like this:



var mySysId = 'xxxxxxxxxxxxxxxxxxxxx'; // put in your sys_id here


var table = new GlideRecord('sys_db_object');


table.query();



while (table.next()) {


      var gr = new GlideRecord(table.getValue('name'));


      if (gr.get(mySysId)) {


                  gs.log('it looks like it came from ' + gr.getValue('label'));


                  break;


      }


}


View solution in original post

17 REPLIES 17

Hi Chuck,



In fact, I am doing something similar but the issue is:


I have a table abc .. and I have my record this table.


And I want to find this record in 'abc' table(gliderecord here) and then insert this record into some other 'xyz' table based on the sys_id of the record (gliderecord here as well).


And I am performing this action on some other SOURCE table.



Can you please help me to accomplish this, I don't want to hard code anything.



Thanks


Jyo


Hi Jyo,



When would you like to copy the contents of the abc record to xyz table? Is it when abc is saved? Changes a particular field (e.g. state) to a particular value? When a UI action/button is clicked?



Can you clarify what you mean by this statement?


And I am performing this action on some other SOURCE table.


Perhaps if you walk me through the scenario of what happens to each record and in what order.


Thanks a ton for your time!


Here is the detail:


So, what   I am trying to do is...


I have a record entry in abc table (say with some fields like ID, Values, Quatity etc) kind of a data entry stored in there, we have an application where in these abc and xyz tables are the child of the SOURCE table which is the parent table.


So, whenever a stage changes say for example 'Project in Progress' then that record entry should be inserted into the xyz table only once. From once I mean no duplicate entries are allowed.


And apparently both abc and xyz tables have same fields but with different names.



And from my statement below, I meant that I am writing a business rule on this table because my stage condition is on this table:


And I am performing this action on some other SOURCE table.



I hope it helps!



Regards


Jyo


Can you share the scripts you are using now to do this? It should be a simple matter of an AFTER business rule to check if the state has been met. If you have the sys_id of the record, then you can do something like this:



var abc = new GlideRecord('abc');


abc.id = 'SOME ID';


abc.quantity = SOME_QUANTITY;



if (abc.get('SYS_ID_OF_ABC_RECORD')) {


        abc.update(); // update existing record


} else {


        abc.insert(); // create new record


}



GlideRecord - ServiceNow Wiki


Something like this:



var mID = new GlideRecord('abc');


mID.addQuery('sys_id', 'f362eefc6f3562806ca1da55eb3ee49c');


mID.query();


if (abc.get('f362eefc6f3562806ca1da55eb3ee49c')) {


  var nonAPLrec = new GlideRecord('u_bom_non_apl');


  nonAPLrec.initialize();


  nonAPLrec.u_sl1 = mID.sys_id ;


  nonAPLrec.u_quoted_quantity_uom_value=mID.u_quoted_quantity_uom;


  nonAPLrec.u_description = mID.u_bom_non_apl_description ;


  nonAPLrec.u_material_id = mID.u_material_id;


  nonAPLrec.u_qty = mID.u_bom_non_apl_qty ;


  nonAPLrec.u_cost_type = mID.u_cost_type ;


  nonAPLrec.u_ext_price = mID.u_customer_price;


  nonAPLrec.u_price = mID.u_price;


  nonAPLrec.u_billable = mID.u_billable;


  nonAPLrec.insert();



}