Client script for Change requests to auto-populate form fields

Rob96
Tera Contributor

Hi

I have a requirement when the configuration item on a change request is chosen with certain value then various fields in the form should be auto populated.

 

I've been looking to achieve this with a client script set to onChange on the configuration item field. I currently have it adding to the blackout plan but this happens for all new configuration item values and not just the required value.

 

Here's my script:

 

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

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

return;

}

//Populate backout plan details

var configitem = g_form.getValue('cmdb_ci').name;

 

      if(configitem = 'Firewall') {           

g_form.setValue('backout_plan', 'This is sample text that will be autopopulated');

     }

}

 

What do I need to amend to get this to only populate when the Firewall record is chosen as the new value?

thanks

 

 

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
return;
}
//Populate backout plan details
var configitem = g_form.getDisplayBox('cmdb_ci').value; //this is how you can get the display value of a reference field on client side
      if(configitem == 'Firewall') {           
g_form.setValue('backout_plan', 'This is sample text that will be autopopulated');
     }
}

 

 

-Anurag

View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
return;
}
//Populate backout plan details
var configitem = g_form.getDisplayBox('cmdb_ci').value; //this is how you can get the display value of a reference field on client side
      if(configitem == 'Firewall') {           
g_form.setValue('backout_plan', 'This is sample text that will be autopopulated');
     }
}

 

 

-Anurag

Jaspal Singh
Mega Patron
Mega Patron

Replace

    if(configitem = 'Firewall') {     

with  

   if(configitem == 'Firewall') {     

 

Ideal, will to use GlideAjax. However, you can try below.

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

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

return;

}

//Populate backout plan details

var configitem = g_form.getReference('cmdb_ci',callback);

 
function callback(configitem)
      if(configitem.name == 'Firewall') {           

g_form.setValue('backout_plan', 'This is sample text that will be autopopulated');

     }

}

Thanks Jaspal this also works as an alternative to Anurag's solution.

You mention that more ideal is to Use GlideAjax though. Why is that?