Checking for value of --none-- in onChange client script

tgutierrez
Tera Contributor

I am trying to clear hide certain variables when a dropdown option is set to --None--. Below is a example of what I tried.

 

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

    //Type appropriate comment here, and begin script below
    if (newValue == '') {
		
		g_form.setDisplay("variable_a", false);
		g_form.setMandatory("variable_a", false);
        g_form.setDisplay("variable_b", false);
		g_form.setMandatory("variable_b", false);
        g_form.setDisplay("variable_c", false);
		g_form.setMandatory("variable_c", false);
		
    } else if (newValue == "new") {
        //clear and hide variable_a and variable_b
        g_form.clearValue("variable_a");
        g_form.setMandatory("variable_a", false);
		g_form.setDisplay("variable_a", false);
		
        g_form.clearValue("variable_b");
		g_form.setMandatory("variable_b", false);
        g_form.setDisplay("variable_b", false);
		
        // display and make mandatory correct variable
        g_form.setDisplay("variable_c", true);
        g_form.setMandatory("variable_c", true);
    } else if (newValue == "modify") {
        //clear and hide variable_c and variable_b
        g_form.clearValue("variable_c");
		g_form.setMandatory("variable_c", false);
        g_form.setDisplay("variable_c", false);
		
        g_form.clearValue("variable_b");
		g_form.setMandatory("variable_b", false);
        g_form.setDisplay("variable_b", false);
		
		
        // display and make mandatory correct variable
        g_form.setDisplay("variable_a", true);
        g_form.setMandatory("variable_a", true);
    } else {
		//clear and hide variable_c and variable_a
        g_form.clearValue("variable_c");
		g_form.setMandatory("variable_c", false);
        g_form.setDisplay("variable_c", false);
		
        g_form.clearValue("variable_a");
		g_form.setMandatory("variable_a", false);
        g_form.setDisplay("variable_a", false);
		
        // display and make mandatory correct variable
        g_form.setDisplay("variable_b", true);
        g_form.setMandatory("variable_b", true);
    }

}

The rest of the code works as intended and I am not even sure if this is the best way to handle what I'm trying to do so any help or feedback would be greatly appreciated. 

Thanks in advance! 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Tgutierrez,

The onChange script isn't getting executed because of the following if condition on top.

    if (isLoading || newValue == '') {  // skip on load and value is empty.
        return;
    }

Remove the "newValue == ''" check

    if (isLoading) {  // skip on load only
        return;
    }

View solution in original post

3 REPLIES 3

Aman Kumar S
Kilo Patron

Best way would be to use ui policies, you need to make multiple ones, but that would be most efficient way.

Best Regards
Aman Kumar

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Tgutierrez,

The onChange script isn't getting executed because of the following if condition on top.

    if (isLoading || newValue == '') {  // skip on load and value is empty.
        return;
    }

Remove the "newValue == ''" check

    if (isLoading) {  // skip on load only
        return;
    }

There's also a need to change the order of setDisplay() and setMandatory() because mandatory fields can not be hidden.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }
    //Type appropriate comment here, and begin script below
    if (newValue == '') {
		g_form.setMandatory("variable_a", false);
		g_form.setDisplay("variable_a", false);
		g_form.setMandatory("variable_b", false);
		g_form.setDisplay("variable_b", false);
		g_form.setMandatory("variable_c", false);
		g_form.setDisplay("variable_c", false);

		
    } else if (newValue == "new") {
        //clear and hide variable_a and variable_b
        g_form.clearValue("variable_a");
        g_form.setMandatory("variable_a", false);
		g_form.setDisplay("variable_a", false);
		
        g_form.clearValue("variable_b");
		g_form.setMandatory("variable_b", false);
        g_form.setDisplay("variable_b", false);
		
        // display and make mandatory correct variable
        g_form.setDisplay("variable_c", true);
        g_form.setMandatory("variable_c", true);
    } else if (newValue == "modify") {
        //clear and hide variable_c and variable_b
        g_form.clearValue("variable_c");
		g_form.setMandatory("variable_c", false);
        g_form.setDisplay("variable_c", false);
		
        g_form.clearValue("variable_b");
		g_form.setMandatory("variable_b", false);
        g_form.setDisplay("variable_b", false);
		
		
        // display and make mandatory correct variable
        g_form.setDisplay("variable_a", true);
        g_form.setMandatory("variable_a", true);
    } else {
		//clear and hide variable_c and variable_a
        g_form.clearValue("variable_c");
		g_form.setMandatory("variable_c", false);
        g_form.setDisplay("variable_c", false);
		
        g_form.clearValue("variable_a");
		g_form.setMandatory("variable_a", false);
        g_form.setDisplay("variable_a", false);
		
        // display and make mandatory correct variable
        g_form.setDisplay("variable_b", true);
        g_form.setMandatory("variable_b", true);
    }
   
}

Execution result

case 1: New

find_real_file.png

case 2: Modify

find_real_file.png

case 3: Else

find_real_file.png

case 4: None

find_real_file.png