Using shared catalog client scripts

John Spencer
Tera Contributor

Hello All,

 

Is there a 'reliable' mechanism to allow 'shared' code between / among catalog client scripts in the same catalog item?

 

That is, a client catalog script would load with the intent of being able to be called by another client catalog script in the same catalog item.  Presently using Yokohama (patch 10).

 

Thanks in advance,

John Spencer

5 REPLIES 5

Juhi Poddar
Kilo Patron

Hello @John Spencer 

 

You could consider using a UI Script to store the shared functions and then call those functions from your Catalog Client Scripts. This is a reliable way to reuse client-side code across multiple scripts and helps avoid duplicating logic.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Ankur Bawiskar
Tera Patron

@John Spencer 

there is no available OOTB mechanism to control shared code between catalog client scripts.

You need to handle this with your development experience and customer requirement and try to avoid conflicting scripts.

Try to keep script in single onLoad always and you can write onChange script on variable based on requirement.
💡 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’s answer is the most practical. While there is almost always a way to accomplish something through customization (for example, calling another Catalog Client Script from a client script), this pattern is generally discouraged. If you find yourself needing it, there is usually a better out-of-the-box approach, or the underlying use case should be reconsidered.

 

Ask first: why is shared client logic required? Often the goal is to avoid repeating the same logic, such as recalculations triggered by multiple onChange scripts.

If you truly need shared logic (e.g., calculations like infrastructure sizing for CPU or memory), a better approach is to implement the logic in a client-callable Script Include and invoke it via GlideAjax. Keep logic in client scripts only when the behavior is purely UI-driven.

Tanushree Maiti
Kilo Patron

Hi  ,

 

In ServiceNow, a UI Script is a reusable block of client-side JavaScript code, similar to how a Script Include works for server-side code. They are used to promote code reusability and maintainability by centralizing common client-side functions like Client Scripts and Catalog Client Scripts.

 

Ref: https://www.servicenow.com/docs/r/api-reference/scripts/c_UIScripts.html

      https://www.servicenow.com/community/itsm-forum/what-is-ui-script-and-give-me-any-example-where-you-...

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: