onchange client script to populate fields with values from another table

carl steele
Kilo Explorer

on the computer table (cmdb_ci_computer), i need to pull the values from the assigned to table which is a reference field of the sys_user table. If the value changes, i need to reflect those changes. 

 

I am using an onchange client script 

 

Presently, the iocation field is being pulled back but not the department field. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var user = new GlideRecord('sys_user');
user.addQuery('department', newValue);
user.addQuery('location', newValue);
user.query();

if(user.next())
{
var userID = user.sys_id;
g_form.setValue('department', userID);
g_form.setValue('location', userID);
}

}

4 REPLIES 4

balaji_charapal
Kilo Guru

Hey,

 

You have to write some thing like below

 

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

  if (newValue == 'Pending') {
    g_form.setValue('u_requeststatus','Incomplete');
  }

}

 

Hope Incomplete, Pending are back end values not labels, give a try with above code 

SanjivMeher
Mega Patron

Please use this script. This onChange client script should run on change of assigned to field.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
  if(newValue){
      var caller = g_form.getReference('assigned_to', popCallerInfo);
   }

   function popCallerInfo(caller){
         g_form.setValue('location', caller.location);
         g_form.setValue('department', caller.department);
   }
}

Please mark this response as correct or helpful if it assisted you with your question.

Shishir Srivast
Mega Sage

I think, you can do this w/o scripting, you can configure the form layout to get the fields related to assigned_to on the form by dot-walking, like below.

 

find_real_file.png

 

find_real_file.png

 

find_real_file.png

find_real_file.pngfind_real_file.png

Shishir Srivastava
 
 
Thanks, i was aware i could dot walk the fields but if reports were done on CMDB_CI_COMPUTER. This would not be present on those fields unless we did a database join. Woudlnt that be correct? I need the data from the sys_user table to be placed on the computer table.