Reuse client side code in portals

cdarsow
Tera Contributor

Hi,
being a Servicenow newbie I wonder, how to create and (re-)use client side JS code for a portal solution.

 

What I thought to understand: create an UI script for shared code and mark it as usable for UI type "Portal":

https://www.servicenow.com/docs/r/xanadu/application-development/scripts/c_UIScripts.html

 

Trying to load that script then within a Catalog client script results in an error as g_ui_scripts seems not to be supported here. Is my assumption right, that this way of loading does not work here?

 

How is the way to provide the UI script in a global way, so that I can access it in record producer scripts (Catalog client scripts) and/or widgets? Actually I just want to provide our own kind of tool-library and don't want to code the same lines over and over again...

 

I really wonder why it is so hard to get to understand how clean coding works in the platform 🙂

 

Thanks for your help,

 

best regards

 

 

 

 

1 ACCEPTED SOLUTION

@cdarsow 

my thoughts

-> Theme JS Includes are safe for shared UI Scripts in Service Portal and commonly used for portal-wide libraries

-> No explicit load-order guarantee from ServiceNow for theme JS Includes; they load as part of the portal page payload before catalog client scripts run

-> For documented load order, use Service Portal Dependencies (JS Includes attached to widgets) where the Order field explicitly controls load sequence

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

5 REPLIES 5

cdarsow
Tera Contributor

As a summerize for myself: a good starting point is to create UI scripts for shared client side JS Code.

 

The usage/including of those can then differ regarding where you want to use in the platform and for what purpose, eg. portal or non-portal usage.

 

For the usage inside catalog client scripts in portal-context, it seems a good way to provide those as theme JS includes. Even though than loaded on every single page of the portal, these should just be loaded once "really" and then be cached by the browser.

 

I keep the UI scripts in my application scope and additionally provide "module scope" inside my JS code to not clutter global scope, eg

 

window.MY_APP_SCOPE = window.MY_APP_SCOPE || {};
window.MY_APP_SCOPE.validation = (function () {
// some shared logic
})();