Stop an onchange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 12:52 PM
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();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 01:03 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 01:03 PM
Hi Josh,
can i know what is your full requirement? what is emailResponse() in that script (is it script include or ui script);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 01:12 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2017 01:58 PM
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.