Dynamically auto populate field based on another field

Martin_samek
Tera Contributor

Hi, I would like to automatically populate value to field when another field value is selected. I added a new field on a incident table called " u_ci_class" and this field is reference field from table 'Configuration Item (cmdb_ci). I want to fill this value when related value 'Configuration item' from the incident table is selected. These values are related to each other in a Configuration Item (cmdb_ci) table. In default, each Configuration Item has own class which I want get. So, what is the best way to make it wokrs? Thank you for your suggestions. Check the attachment for better understanding of my goal.

4 REPLIES 4

Sumanth16
Kilo Patron

Hi @Martin_samek ,

 

create an on-change client script on the incident table and select the "u_ci_class" field.

 

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

  var user = g_form.getReference("requested_for", populateManager);//confirm your field name
}

function populateManager(user) {
  g_form.setValue("manager", user.manager);//confirm your field name and referenced table field name
}  

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

 

Thanks & Regards,
Sumanth Meda

I modified the script in order to make it work for my solution, but the value is still not populated. Could you check the value in my script. The first parameter of getReference() should be the name of field of table where I want to get value from, right? And then, first parameter of setValue() should be the name of field of current form where i want to populate the value?

 

 

 

    var u_class = g_form.getReference('sys_class_name', populateClass); //confirm your field name

}
    function populateClass(u_class) {
        g_form.setValue('u_ci_class', u_class.sys_class_name); //confirm your field name and referenced table field name
    }

 

Hi @Martin_samek ,

 

Did you use the complete script including the below lines?

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

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,
Sumanth meda

nitish99
Tera Guru

Hi @Martin_samek 
Change the custom field type to String then use the below script:

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

  var ci = g_form.getReference("cmdb_ci", populateClass);//configuration item field
}

function populateClass(ci) {
  g_form.setValue("u_ci_class", ci.sys_class_name+'');
} 


Regards,

Nitish