- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2018 11:57 AM
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; //
},
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2018 01:29 PM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 10:21 PM
This error is almost always means you have a problem with recursion in JavaScript code, as there isn't any other way in JavaScript to consume lots of stack. Sometimes calling a recursive function over and over again, causes the browser to send you Maximum call stack size exceeded error message as the memory that can be allocated for your use is not unlimited.
Be considerate while calling functions , also dry run is the best practice to prevent them. It's possible to cause infinite recursion in a fully promisified code, too. That can happen if the promises in a chain don't actually perform any asynchronous execution , in which case control never really returns to the event loop, even though the code otherwise appears to be asynchronous. That's when it's useful to wrap your recursive function call into a -
- setTimeout
- setImmediate or
- process.nextTick
In some programming languages this can be solved with tail call optimization, where the recursion call is transformed under the hood into a loop so no maximum stack size reached error exists.