Reuse Client Script with Service Portal

jleon
Giga Contributor

Hello

I was watching the discussion

Reusing Client Scripts

Now I want to reuse client scripts for catalog items that will work under Service Portal. But we have the issue that the functions declared at the onLoad client scripts, are not recognized by the other client scripts, the functions seems to be mising when are called by other client scripts,

we have the function getNumberOfNights at the OnLoad Client Script BDA Recalc Totals-OnLoad, the mentioned function is being called at a onChange Client Script BDA recalc-Check-in

I am getting this error at the browser(Chrome) debug console

Despite I am using Varset as proposed at the mentioned discussion, I am still getting the error below (when is running under Service Portal)

js_includes_sp.jsx?v=03-22-2017_2340&lp=Wed_Jul_05_10_02_00_PDT_2017&c=12_202:64225 (g_env) [SCRIPT:EXEC] Error while running Client Script "BDA recalc-Check-in": ReferenceError: getNumberOfNights is not defined

How can we work with shared Client Script functions, when we are under Service Portal?

Adding Jim Coyne to the loop

jim.coyne

Thanks!

Jesús

7 REPLIES 7

Hi jleon,

I know this is an old thread but this reminded me of an issue we just recently came across.

Initially we received the same 'undefined' error when defining functions in the onLoad script like so:


function myFunction(){
    // myFunction is undefined
}

 

However, the issue was resolved after declaring the functions as expressions:

myFunction = function(){
    // this works
}   

 

"functions need to be declared as expressions in order for them to work in the Service Portal. They are rendered inside an anonymous function, so they need to be made available to the outside."

http://rubenferrero.com/servicenow/catalog-client-scripts-code-duplication/


In the end, we were able to successfully use functions defined in an onLoad script (in a variable set) and use that variable set wherever we needed to access those functions in other client scripts on various catalog items.

Hopefully this helps someone!

Your reply really helped me 6 years later. Thank you very much! This simple thing was preventing my code from working on Service Portal until I changed it to what you said:

However, the issue was resolved after declaring the functions as expressions:

myFunction = function(){
    // this works
}   

rrshah83
Tera Contributor