Stop an onchange client script

brostoff17
Tera Contributor

I want to stop an onChange client script from running if variables on the form are empty.   However, when I check and uncheck the variable the onChange client script is running on, it does not work even when the variables are populated (email address).

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

      if (isLoading) {

              return;

      }

     

      if (email_address == "")

      return false;      

      else {

              //Calls the catalog client script "OnLoad First Name" which calls the EmaiLAutoGeneration script include

              emailResponse();

              }

}

7 REPLIES 7

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

Where is the value for email_address coming from?   If it is a variable, you need to get the the value using g_form.getValue('email_address').   So you are likely always in your else branch.



Also, where is email response defined.   It's unclear to me if it's supposed to be embedded in the onload client script or a script include.   If it's the former, I don't think you'll have visibility to a function defined within another function.   It will only have local scope within the onLoad script.   You'll need to define that function here.



If it is in the script include, you'll need to make a GlideAjax call to invoke as they are not accessible on the client.


BALAJI40
Mega Sage

Hi Josh,



can i know what is your full requirement? what is emailResponse() in that script (is it script include or ui script);


swarooppydy
Kilo Contributor

Hello Josh,



Client scripts have NO condition field. These scripts run every time the appropriate form is loaded.


Looks like you were running an "onChange". Script continues to run irrespective of value on the attribute whenever you update the field set on the onChange client script. For better performance and if you do not need these scripts to run always - you can run only necessary scripts by adding more conditions within the script.



Please refer to the below link for more detail about Client Scripts -


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




Please hit Like if you find this helpful



Thanks,


Swaroop Pydy


Jyothy,



Below I wrote my script to say, if email address is NOT blank, then run the function (which references a script include).   However, even when email address is empty and I change the value of the variable which the onChange script is running on, the function still runs. It shouldnt run unless the email address is populated, but this isnt working.



Capture.PNG