- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2020 05:20 AM
It looks like a recurring question, but none of the advised solutions seem to work. Therefor I'd like to present my case.
Can somebody help to find out what I am missing ?
(1) UI Script
- application: foo
- name: bar
- API name: foo.bar
- ui type: all
- script
var foo = foo || {};
foo.bar = (function() {
"use strict";
return {
helloWorld: function() {
console.warn("It works !");
},
type: "bar"
};
})();
(2) Catalog client script (also application 'foo'):
- g_ui_scripts.getUIScript('foo.bar') => "No script registered with name ..."
- g_ui_scripts.getUIScript('foo') => "No script registered with name ..."
- g_ui_scripts.getUIScript('bar') => "No script registered with name ..."
- g_ui_scripts.getUIScript('foo.bar.helloWorld') => "No script registered with name ..."
- Same with g_ui_scripts[ ... ] => undefined
- ScriptLoader.runScripts( ... ) => ScriptLoader undefined
- ...
Solved! Go to Solution.
- 5,886 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2020 05:07 AM
! SOLVED !
By carefully debugging the JS code in the catalog item page it became apparent why it doesn't work. The code of the UI script is expected to be executable. The ui script is the result of this invocation. The bottom line is that the proposed default ui script template is invalid for a scope ui script.
My example should therefore be rewritten as:
(function() {
"use strict";
return {
helloWorld: function() {
console.warn("It works !");
},
type: "bar"
};
})();
And the invocation:
g_ui_scripts.getUIScript('foo.bar').then(function(script) {
script.helloWorld();
});
When saving the ui script for the first time there will be a warning about invalid implementation of the ui script. I ignored the error because there is no other way to implement the script for it to be accepted.
Hope this helps other people too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2020 02:36 AM
It's a pity, but it is what it is.
I'd consider it a bug though. Because what's point of allowing scoped scripts which must be included into all web pages ? Especially when the documentation doesn't even mention it. It doesn't make much sense.
It can't be a missing privilege because the catalog script is part of the same application scope.
I'll see how to work around this w/o touching the theme. Because other people are working on it and it would be so easy to include invalid code breaking all web pages.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2020 05:07 AM
! SOLVED !
By carefully debugging the JS code in the catalog item page it became apparent why it doesn't work. The code of the UI script is expected to be executable. The ui script is the result of this invocation. The bottom line is that the proposed default ui script template is invalid for a scope ui script.
My example should therefore be rewritten as:
(function() {
"use strict";
return {
helloWorld: function() {
console.warn("It works !");
},
type: "bar"
};
})();
And the invocation:
g_ui_scripts.getUIScript('foo.bar').then(function(script) {
script.helloWorld();
});
When saving the ui script for the first time there will be a warning about invalid implementation of the ui script. I ignored the error because there is no other way to implement the script for it to be accepted.
Hope this helps other people too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2020 11:33 PM
The invocation example from above is asynchronous. The synchronous invocation example:
g_ui_scripts['foo.bar'].helloWorld();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 09:22 AM
I get an error that g_ui_scripts is not defined when I reference it in an onChange catalog client script, when ordering the catalog item from the desktop interface (UI16, Orlando and Paris). This is consistent with the documentation of ServiceNow which does not list the global variable g_ui_scripts as available. What am I missing?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 04:25 PM
I don't think it works in the desktop interface.
Use different client script types, using Scriptloader for Desktop and g_ui_scripts for Service Portal.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022