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

Default value may also work with this code



  1. javascript:(function executeRule() {  
  2.   var user = new GlideRecord('sys_user');  
  3.   if (user.get(current.variables.facility_manager_name) {  
  4.       return user.employee_number;  
  5.   }  
  6. })();  

Gurpreet07
Mega Sage

Yes , this not the way. Find below script



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


  if(isLoading || newValue == ''){


  return;


}


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


}


The text 'undefined' is not there initially, but when I populate the 'facility manager name' field, the undefined text displays, and no longer displays the Employee Number




Gurpreet07
Mega Sage

Hi Nicole,



In Error message itself , its not showing the newly added lines. Please verify that you have pasted the exact script.


I copied and pasted the exact script, and I'm getting this:



find_real_file.png