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

JG6
Kilo Guru

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
  • ...
1 ACCEPTED SOLUTION

JG6
Kilo Guru

! 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.

View solution in original post

15 REPLIES 15

The SN Nerd
Giga Sage
Giga Sage

What UI is the script running in (Service Portal or UI16?)

ScriptLoader is not supported in Service Portal (https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_ScriptLoaderAPI#r_SCRLDR-ge...).

You have to add the Script to the Service Portal Theme.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

It is running in the service portal.

I can try adding it to the theme. But I'd rather not include the ui script in every page of the application. Will that be the case if it is added to the theme ?

I think you have to add it to the theme, which makes the library available in all portal pages using that Theme.

I'm not sure if there is any other way.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

When creating a "UI Script" in ServiceNow Studio, it's always for a specific application. The description mentions it's typically used for reusable code, callable from other client code.

Maybe this is all a misunderstanding. I assumed this reusable code is only meant for the catalog items of the application (also created with Studio). Isn't that its purpose ?

 

 find_real_file.png