how to get sys_id from one table to another table

Satya8
Mega Expert

Hello everyone,

I have table called 'XYZ' in that i have one reference field, what ever the am choosing the value from that reference field i need that sys_id. How to get it ? i tried below script its not working. I need a background script here.

Script:-

var gr = new GlideRecord('XYZ'); //main table
gr.addEncodedQuery('i put the some encoded query according to my need');
gr.query();
if(gr.next())
{
var grp = new GlideRecord('ABC'); //refernce field table
grp.name = gr;
var rp = grp.getValue('sys_id');
gs.print(rp);

}

Note: But sometimes the reference field will be empty. so at that time i need a sys_id of that particular record and update in the field. the sys_id will be vary for instnaces, so i can't hard core that value.

11 REPLIES 11

Anil Lande
Kilo Patron

Hi,

A reference field always store the sys_id.

For Example, if you have table 'XYZ' in which you create one reference field 'myField' which refers to table 'ABC' then the field 'myField' will always store sys_id of reference record when you sore some value in it.

You can get sys_id using gr.u_my_field.toString() or gr.getValue('u_my_field').

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

yeah its working fine. But sometimes the reference field will be empty. so at that time i need a sys_id of that particular record and update in the field. the sys_id will be vary for instnaces, so i can't hard core that value.

Can you please explain it with scenario?

Are there any conditions which will will help you to get the reference record if the field is empty.

 If you have few records then you can use system property and store sys_id's there and use it in script.

 

Note: In case of multiple records based on condition, store JSON in system property and get the required sys_is matching key.

eg,

{'hardware':'123aabb91b34011038739979b04bc123', 'software':'223aabb91b34011038739979b04bcb68'

}

access property value in using script and parse required sys_id.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Abhijit4
Mega Sage

Hi Satya,

You don't need to glide into reference table.

You can access sys_id directly from reference field.

e.g. if 'Reference to ABC" is field on your 'XYZ table which is referring to ABC table then you can use below script to access ABC record sys_id.

var gr = new GlideRecord('XYZ'); //main table
gr.addEncodedQuery('i put the some encoded query according to my need');
gr.query();
if(gr.next())
{
gs.print(gr.reference_to_abc); // this line will print sys_id of the ABC record referenced in XYZ table
}

 

Let me know if you have any further queries.

Please mark this as Correct or helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

yeah its working fine. But sometimes the reference field will be empty. so at that time i need a sys_id of that particular record and update in the field. the sys_id will be vary for instnaces, so i can't hard core that value.