onChange client script is returning sys_id, but I want the field's value instead
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2020 12:28 PM
Hi everyone,
We have a record producer with a field for employee name. Once the user enters the employee name, we want a separate field to be auto-filled in with that employee's department name. Here is the onchange client script I have written:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('ouc', '');
return;
}
var caller = g_form.getReference('customer', getOUC);
}
function getOUC(caller) {
g_form.setValue('ouc', caller.department);
}
Unfortunately, this script returns the sys_id but not the variable value. I've tried updating the final line as follows, but it didn't help:
g_form.setValue('ouc', caller.department.getDisplayValue());
Any ideas what I am doing wrong? Thanks in advance for any help you can offer!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2020 12:33 PM
It's better to use GlideAjax in your case.
IF you need getReference . use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('ouc', '');
return;
}
var caller = g_form.getReference('customer', getOUC);
}
function getOUC(caller) {
g_form.setValue('ouc', caller.department.name);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2020 06:37 AM
Hi Sachin,
Thank you for the reply. I've read GlideAjax is the way to go but I haven't worked with it before. How would the code look different if I used GlideAjax?
Scott

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2020 08:37 AM
Please use below code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('ouc', '');
return;
}
var caller = g_form.getReference('customer', getOUC);
}
function getOUC(caller) {
g_form.setValue('ouc', caller.department.id);
}
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2020 08:50 AM
Hi,
you can only dot-walk once in client script using getReference(); so you cannot get the id value using below
caller.department.id
so please use Client Script + Script Include + Glide Ajax approach
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader