Reusable functions in Catalog Client Script

victor21
Tera Guru

Hello All,

I use to define general functions in onLoad Client Script which are later used in onChange Client Scrips - but this does not work for Catalog Client Scripts !.

Does anyone know how this can be achieved for Catalog Client Scripts?

Thanks and best regards,

Victor

1 ACCEPTED SOLUTION

victor21
Tera Guru

Got to the issue after some investigation; I needed to uncheck the Isolate script checkbox - although I am not doing any DOM manipulation.

Regards,

Victor

View solution in original post

4 REPLIES 4

Harsh Vardhan
Giga Patron

you can define that function inside UI Script and then you can use it in your catalog client script or any client side script. 

 

Kindly have a look on below doc link for further details about UI Script

https://docs.servicenow.com/bundle/newyork-application-development/page/script/client-scripts/concep...

victor21
Tera Guru

Got to the issue after some investigation; I needed to uncheck the Isolate script checkbox - although I am not doing any DOM manipulation.

Regards,

Victor

This helped me as well. It was sufficient to uncheck it for the "onLoad" client script. All "onChange" client scripts could stay with checked "Isolate script" to use the function from "onLoad".

I have also defined the function outside of the "onLoad" function as defining it inside did not work for me (Orlando release):

function onLoad() {}

function myNewReusableFunction() {}

I defined the function at "onLoad", out side of the onLoad() function. I called it at onChange, but it threw this error: "Error while running Client Script "...": ReferenceError: [function_name] is not defined". "Isolcate script" onLoad is unchecked. It shows the error even I checked or unchecked the "Isolate script" at onChange script.