onChange client script returning 'undefined'

lando321
Tera Contributor

Hello all,

 

I am trying to populate the dept code with an onChange client script, but it keeps returning 'undefined' when i attempt to dot walk to the id field on the department table. When i use getDisplayValue() the field does not return anything at all. Would greatly appreciate any help.

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
if (newValue){
	var caller = g_form.getReference('name', popDept);
}
	function popDept(caller){
  g_form.setValue('department_code', caller.department_id);
	}  
}

 

1 ACCEPTED SOLUTION

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

So it is basically you have to dot walk two times, please use the below code:-

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
       return;
   }


   var caller = g_form.getReference('name');
   var dept = caller.department;
   if (dept) {
       var grDept = new GlideRecord('cmn_department');
       if (grDept.get(dept)) {
            g_form.setValue('department_code', grDept.department_id);
       }
   }
}

 

 

Please mark my answer as correct based on Impact.

 

View solution in original post

10 REPLIES 10

Claude DAmico
Kilo Sage

Is the reference field you are trying to dot-walk through the correct field? Looking at your screenshots, it looks like you should be doing g_form.getReference('department', popDept) instead. This line is saying, "get the referenced record in this field for me to use" and I'm not seeing a 'name' field shown unless the label is "Department" and the underlying field name is "name".

Claude E. D'Amico, III - CSA

Hi @Claude DAmico 

'name' is the underlying field name to the requested for reference field. The requested for field is the reference field to the sys_user table that contains the department name, should i not be doing it this way?

Claude DAmico
Kilo Sage

Since that is the case, getReference() is currently retrieving the sys_user record which you would want to dot-walk to the department and further to the department ID (i.e. caller.department.department_id in your script.)

Claude E. D'Amico, III - CSA

Attempted that change, but its still returning an 'undefined' value in the department code field.