onChange client script on multiple choice variable HELP!

johnnyw
Mega Expert

I have a simple client script that I want to run on a catalog item that I can't seem to figure out.   Need your help!

I have a variable called "access_type" that is a multiple choice type.   I want to have an onChange script to display a field message if the "network" option is selected.   So I created this script:

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

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

          return;

    }

    //Shows field message if network is selected.

if(newValue + "" == "network" && oldValue != newValue){

      g_form.showFieldMsg("access_type","This form is to request NEW access to network folders and files only.","info");

}

else{

g_form.hideFieldMsg("access_type",true);

  }

  }

The script works, but the problem I'm experiencing is if I click the "network" option multiple times even after it is already selected, another message pops up.   I can't seem to figure out how to prevent multiple messages from appearing.   Advice anyone??

1Click.jpg

Clicked "network" 3 times without choosing another option.

2Click.jpg

Thanks,

Johnny

1 ACCEPTED SOLUTION

Steve McCarty
Mega Guru

Johnny,



Change the script to always hide the field message first and then only add it back if the choice is set to network.   Like this:



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


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


          return;


    }


    //Shows field message if network is selected.


g_form.hideFieldMsg("access_type",true);


if(newValue + "" == "network" && oldValue != newValue){


      g_form.showFieldMsg("access_type","This form is to request NEW access to network folders and files only.","info");


}


  }



-Steve


View solution in original post

4 REPLIES 4

Steve McCarty
Mega Guru

Johnny,



Change the script to always hide the field message first and then only add it back if the choice is set to network.   Like this:



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


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


          return;


    }


    //Shows field message if network is selected.


g_form.hideFieldMsg("access_type",true);


if(newValue + "" == "network" && oldValue != newValue){


      g_form.showFieldMsg("access_type","This form is to request NEW access to network folders and files only.","info");


}


  }



-Steve


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Johny,



Replace if(newValue + "" == "network" && oldValue != newValue) with



Var net = g_form.getValue('your variable name');


If(net == 'network')


{


}


Hi Pradeep,



Thanks for your advice, but this did not work.   The behavior was the same after this update.



Regards,


Johnny


johnnyw
Mega Expert

Thanks Steve!   That worked.