How to fix a "RangeError: Maximum call stack size exceeded" error?

patricklatella
Mega Sage

Hi all,

I'm getting a javascript error on a catalog item in service portal...the error is showing as "RangeError: Maximum call stack size exceeded".  Anyone see what would be causing this?  This code is taking an entered numeric value and converting it to a different currency value.  Thanks!

 

My catalog client script is:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

//Take entered value in the u_currency_amount2 field and validate
g_form.clearValue('u_annual_revenue'); //added by Patrick Latella; 
var code = g_form.getValue('u_currency_code');
var am= newValue.replace(/[^0-9]/gi,'');
var amo = am.trim();
var ga = new GlideAjax('CurrencyConversion');
ga.addParam('sysparm_name','convertAmount');
ga.addParam('sysparm_code',code);
ga.addParam('sysparm_amount',amo);
ga.getXML(conversion);

function conversion(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
answer=Number(answer);
g_form.setValue('u_annual_revenue',answer);
}
}

and my script include function is:

convertAmount : function(){
var currency = this.getParameter('sysparm_code');
var amount = this.getParameter('sysparm_amount');
var eur = '';
var eurFixed; // 
var gr = new GlideRecord('u_intake_currencies');
gr.addQuery('u_currency',currency);
gr.query();

if(gr.next()){
eur = (amount * gr.u_value_in_euro)/100;
eurFixed = eur.toFixed(2); // 
}

return eurFixed; // 
},

1 ACCEPTED SOLUTION

thanks for that tip...I actually just figured out that it's not running the function over and over again, it's actually re-running the client script over and over again.  So I just added the bold logic below and now it works.  

if (isLoading || newValue === '' || oldValue == newValue) {
return;
}

View solution in original post

10 REPLIES 10

Hello Ahmmed,

checking back in for help on the "Maximum call stack size exceeded" error I'm getting on a catalog client script.  I've isolated the problem to somewhere between the lines indicated below.  And what it's doing is running this bit of script indefinitely, it's stuck in a loop.  See my screenshot of the console logs. Do you see the error in the script?  Thanks!

 

find_real_file.png

 

var putThousandsSeparators;
console.log('it is breaking after this');

putThousandsSeparators = function(value, sep) {
if (sep == null) {
sep = ',';
}
// check if it needs formatting
if (value.toString() === value.toLocaleString()) {
// split decimals
var parts = value.toString().split('.');
// format whole numbers
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, sep);
// put them back together
value = parts[1] ? parts.join('.') : parts[0];
} else {
value = value.toLocaleString();
}
if(value.indexOf('.')==-1) {
value=value+'.00';
console.log('value is '+value);//this is getting logged after the call stack error stops the script and allows it go on
}
return value;

};
console.log('it is breaking before this');

g_form.setValue('u_currency_amount',putThousandsSeparators(newValue));
}

Thanks Ahmmed this was the fix for me!

patricklatella
Mega Sage

upon further investigation, I'm seeing that what's happening is that the line 

g_form.setValue('u_currency_amount',putThousandsSeparators(newValue));

is triggering the catalog client script to run again, and that's what causing the loop behavior.  So I need the script to execute, but for it to not trigger the catalog client script to run again when the field gets set...which the system is treating as the field changing again.

Gosia Urbanska
Giga Guru

I had a similar issue and in my case setting "control.value" helped 

if(control == null){
g_form.setValue('field',VLAUE);
}else{
control.value = VALUE;
}

thanks for that tip...I actually just figured out that it's not running the function over and over again, it's actually re-running the client script over and over again.  So I just added the bold logic below and now it works.  

if (isLoading || newValue === '' || oldValue == newValue) {
return;
}