Hide / Show Fields using client script

Sam Motley
Giga Guru

Hi all, 

 

We've been asked to show a field and hide another field if a certain cmdb_ci class has been chosen when raising a change. 

In this case the class the sys_cmdb_netgear. 

Normally we'd do a ui action for this but it needs to happen when the config item has been chosen as the change is requested when pressing submit. 

 

I'm trying to query the class when the config items is selected but doesn't work. 

below is the script i'm trying to work with: 

=========

function onChange (control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
if (g_form.cmdb_ci.sys_class_name('cmdb_ci_netgear')) {
 
  g_form.setVisible('u_network_device', true);
  g_form.setVisible('u_configuration_items', false);
alert("class is true");
 }
 else {
  g_form.setVisible('u_network_device', false);
  g_form.setVisible('u_configuration_items', true);
alert("class is false");
 }
}
=========
Can anyone help me understand why this isn't working please? 
Thanks as always 
1 ACCEPTED SOLUTION

@Sam Motley Could you try with the following code.

 


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ci = g_form.getReference('cmdb_ci', getCIDetail);

    function getCIDetail(ci) {

        if (ci.sys_class_name == 'cmdb_ci_netgear') {
            g_form.setDisplay('u_network_device', true);
            g_form.setDisplay('u_configuration_items', false);
            alert("class is true");
        } else {
            g_form.setDisplay('u_network_device', false);
            g_form.setDisplay('u_configuration_items', true);
            alert("class is false");
        }
    }
}

View solution in original post

12 REPLIES 12

Sandeep Rajput
Tera Patron
Tera Patron

@Sam Motley You will not be able to access the class name property directly using a g_form object. Instead use getReference method to get the details of the reference object.

 

var ci = g_form.getReference('cmdb_ci', getCIDetail);

function getCIDetail(ci) {

g_form.setValue('ci_name', ci.name);

}

hi @Sandeep Rajput thanks for checking out my issue, could i ask what g_form.setValue('ci_name', ci.name); is doing please? 

@Sandeep Rajput i've tried creating the following but still not working as expected: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
{
var ci = g_form.getReference('cmdb_ci', getCIDetail);
 
function getCIDetail(ci) {
 
g_form.setValue('ci_name', ci.sys_class_name);
 
}
if (ci_name == 'cmdb_ci_netgear'){
//if (g_form.cmdb_ci.sys_class_name == 'cmdb_ci_netgear') {
 
g_form.setDisplay('u_network_device', true);
g_form.setDisplay('u_configuration_items', false);
alert("class is true");
} else {
g_form.setDisplay('u_network_device', false);
g_form.setDisplay('u_configuration_items', true);
alert("class is false");
}
}
}

@Sam Motley Could you try with the following code.

 


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ci = g_form.getReference('cmdb_ci', getCIDetail);

    function getCIDetail(ci) {

        if (ci.sys_class_name == 'cmdb_ci_netgear') {
            g_form.setDisplay('u_network_device', true);
            g_form.setDisplay('u_configuration_items', false);
            alert("class is true");
        } else {
            g_form.setDisplay('u_network_device', false);
            g_form.setDisplay('u_configuration_items', true);
            alert("class is false");
        }
    }
}