- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 06:44 AM
I have a variable titled 'Facility Manager Name' (facility_manager_name) which is a reference field that allows the requester to look up and select a person. I would like to auto populate the 'Employee Number' (emp_number) field based on the person selected. I have tried the following Client Script, but I am doing something wrong:
function onChange(control, oldValue, newValue, isLoading) {
var id = g_form.getValue('facility_manager_name');//replace 'u_first_field' with the name of your reference field.
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',id);
user.query();
if ( user.next() ) {
g_form.setValue('emp_number', user.field_on_sys_user);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 07:06 AM
Replace g_form.setValue('emp_number', id.field_on_sys_user); with g_form.setValue('emp_number', id.emp_number);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 09:23 AM
Add below code in beginning of onChange function
if(isLoading || newValue == ''){
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 09:41 AM
I must be adding in the wrong place? Keep getting error.
function onChange(control, oldValue, newValue, isLoading) {
if(isLoading || newValue == ''){
var id = g_form.getReference('facility_manager_name');//replace 'u_first_field' with the name of your reference field.
g_form.setValue('emp_number', id.emp_number);
return;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 06:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 07:03 AM
Geoffrey,
This script returns my own employee number, and not the employee number of the selected person.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2016 07:07 AM
Oh, sorry 😕 I didn't read the question properly.
Then you should be using a GlideAjax query: http://wiki.servicenow.com/index.php?title=GlideAjax