onChange client script is returning sys_id, but I want the field's value instead

scottn
Mega Expert

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!

7 REPLIES 7

sachin_namjoshi
Kilo Patron
Kilo Patron

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);
}

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

 

 

 

 

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

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader