- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2025 07:39 AM - edited 02-26-2025 07:57 AM
I have found a couple of posts on here about calling a named function from an onLoad script in an onChange in a form (example below). It is not working for me and I am wondering if it doesn't work with record producers or if I am missing a setting.
I get Uncaught ReferenceError: calledFunction is not defined
Edit: I have a complex set of hide/show rules given to me by our customer that will require multiple onChange scripts and I'd like to have all of them call a singular function to reduce the amount of code I have to copy into all of them.
function onLoad() {
// Blank since we're just creating a shared function
alert("loading");
}
// Shared function that can be accessed by any other client script
function calledFunction() {
alert("Called");
}
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert("change running");
calledFunction();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 09:19 PM
Hello @CP-UW
I have created a Record producer. Please check the screenshots below of the Onload and Onchange scripts.
OnLoad Script:
OnChange Script:
Result:
Try this code in your instance and let me know.
If it helped, please accept my response as a solution and mark it as helpful
thanks!
Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2025 07:23 PM
Hello @CP-UW
Glad to hear that it worked. If you can mark my answer as helpful it would be great!
Thank You!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2025 09:57 PM
Hello @CP-UW ,
The error "Uncaught ReferenceError: calledFunction is not defined" suggests that the calledFunction is not accessible in the context where it's being called. This could be due to the scope of the function or the way the scripts are being loaded.
In ServiceNow, client scripts (like onLoad, onChange, etc.) are typically run in the context of the form they are attached to. If you're trying to share a function between scripts, you need to ensure that the function is accessible in the global scope.
ON LOAD:
function onLoad() {
alert("loading");
}
function calledFunction() {
var ga = new GlideAjax('GlobalFunctions');
ga.addParam('sys_id', g_form.getParameter('sys_id'));
ga.addParam('functionName', 'calledFunction');
ga.getXMLAnswer(function(response) {
alert(response.responseText);
});
}
ON CHANGE:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert("change running");
calledFunction();
}
Please mark this as correct if this resolves your issue.
Regards,
Debasis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 07:55 AM
It is in the same form. Adding the code above did not fix it. I think it may not work in record producers. We have this functionality working on regular forms, but not record producers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2025 10:47 PM
this will work in native but I doubt if it works for portal.
Better use onChange as onLoad and have logic inside it
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader