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

Jaspal Singh
Mega Patron
Mega Patron

Is 'OUC' not a reference field? If not you can try using caller.department.name

Hi Jaspal,

OUC is the label name but the actual field name in the cmn_department table that I want is the 'id' field.

Harsh Vardhan
Giga Patron

you can solve this requirement either you change your "ouc" field as reference variable and then populate the sys_id in ouc variable. 

or other way would be , create glide ajax. to get the display name of the value. because call back-function will only work at one level of dot walk. 

 

glide ajax is not so hard , if you are new i would suggest go through the doc link and then write the script . 

 

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...