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-03-2022 04:42 AM
Hi,
Your question is not quite clear.
If reference field is empty then how could we get its sys_id?
If you are trying to say that the field is showing value as empty but in other instances it shows reference record properly then the logic behind this is, if reference record does not exist in instance then on the form, it shows you field value as empty but if you try to get that field value with script then you will get sys_id of reference record even if reference record doesn't exist in instance.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2022 04:57 AM
Hello,
You want the sys_id of the record that the reference field is empty ?
If it so , you can try this 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()) // after you test the script, replace if by while to get all desired records
{
if(gs.nil(gr.name_field)){
gs.info("Refrence field is empty for that record : "+gr.sys_id);
}
}
Regards,
Hajar