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

Ankur Bawiskar
Tera Patron

@cdarsow 

check below

[SOLVED] How to call scoped UI script from catalog client script ? 

Avoiding code duplication in Catalog Client Scripts 

also this

Call UI script from an Onload Catalog client script  

Service Portal UI Script or Reusable functions in Client Scripts 

💡 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

 @Ankur Bawiskar Thanks for your reply and the linked possibilties.

 

So, it is safe to have the UI scripts as a JS include in the theme?

 

Just wonder if its somehow guarenteed that the UI scripts are really loaded when we use them inside the catalog item scripts then. 

The g_ui_scripts approach seemed more reliable with the returned promise. (but not working for whatever reason in the catalog client scripts)

 

Maybe you know some documentation on loading order of the JS code?

 

Thank you!

@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

Tanushree Maiti
Tera Patron

Hi @cdarsow 

 

You are on correct path.

To create and reuse client-side JavaScript in a ServiceNow Service Portal, the most effective method is using UI Scripts. These act as the client-side equivalent of Script Includes, allowing you to package logic that can be shared across multiple widgets or pages

 

Refer:  Servicenow Documentation: UI scripts 

 

https://www.youtube.com/watch?v=fyMA-E-kouY

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti