The CreatorCon Call for Content is officially open! Get started here.

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

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Can you paste the screenshot of the name field?

 

Is it a reference field to the user table if the name is the correct field and on what field is the onchange script triggering?

 

Sure @Saurav11 

 

lando321_0-1665680472997.png

 

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.

 

Im still getting 'undefined' and i am cueing the onChange from the name variable on the form. Does it matter that this catalog client script is in a variable set?

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
//if (newValue){
	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.id);
		}
	}
}

One small tweak, worked for me, swapped out department_id with just "id"