Library Functions for Catalog Client Script - Is it really this difficult???

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 07:18 AM
Use Case
I wrote some fancy code for handling currency input from the user. On the portal, on catalog items, this code lets the user enter values in various formats, such as 1000, $1000, $1,000, and $1,000.00, and no matter what format they use, it converts the result to $1,000,00 so it is easily identifiable as a currency and is nicely formatted with commas where a US-based user would expect them to be. The code is designed to be attached to OnChange() catalog client scripts. I want to write the code ONCE and have it available to all my OnChange() catalog client scripts.
(I DO NOT WANT TO USE REGULAR EXPRESSIONS OR ANYTHING THAT SERVICENOW HAS PROVIDED. I WANT TO USE MY OWN, FANCY CUSTOM CODE. THANK YOU.)
Here is my Question
What are the steps to make a simple function like this available EVERYWHRE in my catalog item? Is it really this difficult??? Or am I doing something wrong??? Here is what I did:
1. Created multiple Catalog Client OnChange() Scripts. These are the "clients" of my fancy function.
Table catalog_script_client
2. Created a new Variable/Question of type Custom
Table item_option_new
Use the type specification tab to point this to the Widget
3. Created a Widget
Table sp_widget
Linked this to a "Dependency" using table m2m_sp_widget_dependency (see step 4)
4. Created a Widget Dependency
Table sp_dependency
Pointed this to a "JS Include" file
5. Created a JS Include record
Table sp_js_include
Points to the UI Script
6. Created a UI Script
Table sys_ui_script
Here is where I declared/defined my fancy reusable function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 01:35 PM
I'm assuming you also added the variable of type Custom to all Catalog Items in which the "client" Catalog Client Scripts reside?
Also, did you mark the "Include on page load" checkbox on the Widget Dependency (sp_dependency) record?
Other then that, what you describe, should work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 05:33 AM - edited 09-25-2023 05:33 AM
Oh, the code IS working. And Yes, I added the Variable to my Catalog Item.
And Yes, I did check the "Include on Page Load" checkbox.
But that seems like an awful lot to make people go through just to have some client-side reusable script. Is that really the official / correct process? Or is there a much easier way that I'm totally unaware of?
Thanks, my friend. @-O-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 06:25 AM
You're welcome.
And I'm afraid, yes, there is no easy way to include client side libraries, other then as dependency in portal or by using Formatters in Core UI/CMS.
That said one could make the UI Script a dependency of the catalog item widget, then one would not need to create a variable of type Custom or variable set with a variable of type Custom and include that into each Catalog Item separately.
Or one could create a UI Script and mark it as being Global, but in that case the UI Script will load everywhere, even where it was not intended to, slowing down all pages.
I guess what SN is trying to say, the subliminal message is that one should NOT extend the system as much as possible with client side scripts/libraries, but one should stick to what is available OOB.
Sometimes is not even subliminal, but outright explicit.