The CreatorCon Call for Content is officially open! Get started here.

Record Producer onLoad Catalog Client Script Function with onChange

CP-UW
Tera Contributor

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();
}

 

 

 

 

1 ACCEPTED SOLUTION

Hello @CP-UW 

I have created a Record producer. Please check the screenshots below of the Onload and Onchange scripts. 

OnLoad Script:

RushiSavarkar_0-1740633339059.png

OnChange Script:

RushiSavarkar_1-1740633392023.png

Result:

RushiSavarkar_2-1740633475636.png

RushiSavarkar_3-1740633525756.png

 

 

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!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

View solution in original post

8 REPLIES 8

Hello @CP-UW 

Glad to hear that it worked. If you can mark my answer as helpful it would be great!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

Debasis Pati
Tera Guru

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

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@CP-UW 

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.

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