How can I auto populate an employee ID based on the selected user?

nicolemccray
Tera Expert

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

}  

}

1 ACCEPTED SOLUTION

Replace g_form.setValue('emp_number', id.field_on_sys_user); with   g_form.setValue('emp_number', id.emp_number);


View solution in original post

15 REPLIES 15

Gurpreet07
Mega Sage

Hi Nicole,



Below script should work for you



function onChange(control, oldValue, newValue, isLoading) {


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.field_on_sys_user);


}


Gurpreet,


This returns the text 'undefined' in the 'Employee Number' field.


Replace g_form.setValue('emp_number', id.field_on_sys_user); with   g_form.setValue('emp_number', id.emp_number);


Thanks Gurpreet!   This worked.   However, the 'Employee Number' field still displays the 'undefined' text until I search for and populate the 'Facility Manager Name'.   Any idea on how to remove this?