Checkbox onChange catalog client script on Service Portal

Karan Sharma3
Tera Expert

I created a test Service Catalog with 2 variables. One checkbox and other as single line text. Created a catalog UI Policy to show single line text when checkbox is checked or true. Then an onChange catalog client script of checkbox to alert true if checkbox if true and alert false if checkbox is false.

Then I went to service portal opened the service catalog, clicked on checkbox, the single text field appears and the first alert comes as true.

To the surprise the alerts never stops. It keeps on running again and again.

Next thing I tried the same with only a difference of variable type as yes/no. Everything worked fine and there was only one true alert.

Is this a Service portal bug with onchange catalog client script for checkbox?

3 REPLIES 3

Gurpreet07
Mega Sage

That may be a product issue but you should be able to bypass it using below code at beginning of script.



if(newValue == oldValue){


return;


}


This solution looks to be working but when you add another alert in your code you will see that the client script keeps on rendering again and again which is not a best programming method.



if(newValue == oldValue){


alert('return');


return;


}



This will give you infinite loop of return alert.


Venkateswarlu K
Mega Guru

Hi Karan i think it is scripting issue may be am also agree with Gurpreet


For better coding u can follow the coding best practices that will prevent this type of issues for above issue u can follow this code


for more information http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#gsc.tab=0



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


    if (isLoading)


    return;


    if (newValue) {


    if (newValue != oldValue) {


//write ur code here


          }


    }


}