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

Hi Josh,



calling a script include in client script, you need to use GlideAjax please check this link below


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


Hi Josh,



Couple of things to be taken care of as per the screen shot shared by you:



1) OnChange Script works instantly whenever the value of the Variable or field on which the Script has been written is changed, without any need of saving the form to see the changes. For example in your Scenario, you want whenever the Value of the Variable say Email is empty the function should not Run, but since when you check the Value of the Variable Email the value no longer remains as Null or Empty and the Script gets called thereby calling your Script Include.



2) Secondly, In Line Number 6 you are fetching the value of the Email Variable using getValue function but not storing that Value in any variable to use it later in any stage of the script like as you are using/checking in your IF Block in Line Number 8, which i don't think so is correct. You need to fetch the Variable   Value , store it in a variable and then use it in in the IF block as needed or you can directly use GetValue function in the IF block as below:


if(g_form.getValue('Variable_Name')=='')


{


...... Code you want to Execute


}




3) Last, As Explained above by Balaji above it would be better to Use Glide AJAX if you are calling a Servide SIde Function(script include) in to a Client Script. You can simply pass the Variable value on which you want to check to the server side have the validations there and based on the result returned do your client side validations.



Below links would be useful on how to use Glide Ajax in client side scripts:



SN Pro Tips — GlideRecord & GlideAjax: Client-Side Vs. Server-Side


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


Examples of asynchronous GlideAjax


GlideAJAX API




Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

shloke04
Kilo Patron

Hi,



Did you got a chance to review the solution proposed, if your query is Resolved please mark the answer as correct and close the thread.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke