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

Ahmmed Ali
Mega Sage

Hello,

 

We ran into same issue in service portal for a catalog item.

in our case we have a variable set included in the item. in the variable set there was a variable with same name as of variable set name. after updating the variable name, the issue was resolved.

I would suggest have an alert in the code and debug which statement is causing the issue.

 

hope this helps.

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ahmmed, thanks for the reply.

There are not 2 variables with the same name on the catalog item, and there is not a variable set, however there are 2 onChange catalog client scripts on the same variable...they run 2 different functions.  I've deactivated one of them, and am still getting the Javascript Error 

find_real_file.png

So now looking at the 1 catalog client script that is still active, which is also showing the error as a "RangeError: Maximum call stack size exceeded"...this is that script, which is taking an entered number value and adding the thousands commas where needed.  See anything here that would throw the error in Service Portal?  thanks

 

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

var isValid = /^[0-9,.]*$/.test(newValue);
if(!isValid) {
alert('Please enter only valid amount in Amount.');
g_form.setValue('u_currency_amount','');
g_form.setValue('u_annual_revenue','');
}
else {

var putThousandsSeparators;

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';
}
return value;
};

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

}

Hello Patrick,

 

I tried above code in my developer instance and it worked perfectly!

I would suggest to have a alert in the code and check which statement exactly is causing the issue. then we can find the solution.

I also tried the same approach like have a alert in code and check the order of execution and till where the code is executing successfully.

 

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ahmmed, considering the code is still performing its function correctly, I'm confused how to implement an alert to pinpoint the problem...can you show me an example of how I would the alert in my script?