how to get sys_id from one table to another table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2022 10:02 PM
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.
- Labels:
-
Script Debugger
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2022 10:12 PM
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
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2022 11:26 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2022 11:42 PM
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
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2022 10:27 PM
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
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2022 11:27 PM
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.