onChange script error: RangeError: Maximum call stack size exceeded function () { [native code] }

MK-p
Tera Contributor

Getting below error in onchage client script.

 

onChange script error: RangeError: Maximum call stack size exceeded function () { [native code] }

 

code which i am using.

var regex = /^(1[0-3]|[1-9])$/;
    
    if (!regex.match(newValue)) {
        g_form.showFieldMsg('xxx', 'Please enter a number between 1 and 13.', 'error');
        g_form.setValue('xxx', 0); 
		
    }
2 ACCEPTED SOLUTIONS

Hey @MK-p ,

 

Clear Value clears value as well as messages related to the field, can you try adding error message post clearing value as below.

 

var regex = /^(1[0-3]|[1-9])$/;
    
    if (!regex.match(newValue)) {
        g_form.clearValue('xxx'); 
        g_form.showFieldMsg('xxx', 'Please enter a number between 1 and 13.', 'error');
    }

 

 

 

---------------------------------------------------------------------------------------------------

Please mark my answer as helpful/correct if it resolves your query.

Thanks,
Nilesh Wahule

---------------------------------------------------------------------------------------------------

 

View solution in original post

Hi @MK-p ,

 

Put clear message before field message.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@MK-p 

it's because it's going inside a loop.

you are validating the value with Regex and again setting it which in turns trigger client script

try this

var isUpdating = false; // Declare a flag

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || isUpdating) {
        return; // Exit if the form is loading or if the script is already updating the field
    }

    var regex = /^(1[0-3]|[1-9])$/;

    if (!regex.test(newValue)) { // Use regex.test() instead of regex.match()
        g_form.showFieldMsg('xxx', 'Please enter a number between 1 and 13.', 'error');
        
        isUpdating = true; // Set the flag before updating the field
        g_form.setValue('xxx', 0); 
        isUpdating = false; // Reset the flag after updating the field
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MK-p 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MK-p 

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader