Stack Overflow

Community Alums
Not applicable

I am using an onChange() Client Script to round the entry of a field to the next highest, even, hundredth based upon entry of data into the field. I am running into a stack overflow message,

onChange script error: RangeError: Maximum call stack size exceeded function (){var o=i(m,arguments);return l.apply(n,o)}     

In the API is suggests checking the oldValue against the newValue to make sure not to unnecessarily run the onChange(). This is what I have, and I can't seem to work it out. Your help is appreciated.

function onChange(control, oldValue, newValue, isLoading, isTemplate) { 
        if (newValue === oldValue){ return;

        }
else if (isLoading || newValue === '') {
                  return;
       
        }
else{
                  if (newValue) {
                            if (newValue != oldValue){  
                                      var round = ((Math.ceil(newValue * 100) /100); g_form.setValue('nitrate_0_50', round);
                            }
                  }          
        }
4 REPLIES 4

Mike Allen
Mega Sage

In this line: }else if (isLoading || newValue === '') {   try to stay away from explicit typing (the triple equals).   I have found the ServiceNow does not tend to like it, and often errors on it.


Community Alums
Not applicable

Good to know, that's the default code for the function.


They may have made changes in Geneva, but it would do terrible things to me in Fuji.  



I think that this:



if (newValue) {


                            if (newValue != oldValue){



is redundant.   You are already saying if they are equal, return; so, you shouldn't need if (newValue != oldValue){ .   You are only firing this if the value changes, so I think you wouldn't need this:if (newValue) { .


dave_edgar
Mega Guru

I have the same error.   We have this error in our Geneva environment but not out Fuji environment.



Here is our script.   So our scenario is when we enter a User(caller) in to a field we specifically pull back (setValue) one of 4 possible email addresses, depends on the user.   You see we support retails stores that could have 4 email addresses.   So we bring back the main store email address (caller.u_4th_email) but if not present or it;s not a Store we get the main caller email address (caller.email).   We have the error though even if we change what field we want to pull back from the caller/user record.



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


      if (isLoading) {


              return false;


      }


      if (!g_form.getControl("caller")) {


              return false;


      }


      var caller = g_form.getReference("caller", setContactDetails);


}


function setContactDetails(caller) {


  var type = g_form.getValue("caller_type");


        if (caller) {


  if (type == "store") {


  g_form.setValue("store_email_address", caller.u_4th_email);


  } else {


  g_form.setValue("store_email_address", caller.email);


  }


  g_form.setValue("contact_number", caller.phone);


      }


}



Can anyone spot the issue.   Or propose a change to the script so this works for Geneva?