Unable to call an UI script from client script within scoped application

priyannka
Tera Contributor

Hello,

I am working on a scoped application and i was trying to call an UI script function from a client script in the same scope but i am not able to do it.Please find the script which i am using below -

UI script- 
Name - clientUtil

var x_roho_c2r = x_roho_c2r || {};
x_roho_c2r.clientUtil = (function() {
"use strict";
function giveAlert(){
alert('alert from cilentUtil...!!!!');
}
return {
type: "clientUtil"
};
})();

 

Client script -

function onLoad() {
//Type appropriate comment here, and begin script below
alert('onLoad Client script..!!!');
ScriptLoader.getScripts('x_roho_c2r.clientUtil.giveAlert()', function(){
x_roho_c2r.clientUtil.giveAlert();
 alert('testing loop second');

 });

Any help please?

5 REPLIES 5

Raj68
Mega Guru

Hi priyanka,

please go through below link hope it will help you:

https://docs.servicenow.com/bundle/london-application-development/page/app-store/dev_portal/API_reference/GlideUIScripts/concept/GUIScriptsAPI.html

 

NOTE: Mark correct or helpful if it helps you.

 

Warm Regards,

Raj patel

 

I recommend that ServiceNow either take down this particular documentation or fix it.  It does not work..certainly not within a Scoped Application.

I've created a HI Ticket citing this very link and asking the same simple question as @priyannka .. but the HI ticket was closed saying they do not offer professional services consultation.

HELLO...SN...we're not asking for that...correct the documentation or remove it because I cannot find anyone with information on how to make this work.

Furthermore, I have questions about the proper way to call UI Scripts from a Scoped Application that is a Store application and this capability is needed for both Classic UI (forms) as well as for Service Portal.

Anyone with insights...your feedback is most appreciated.

Ishita Shrivast
Kilo Guru

Hi Priyanka, try writing it like this

ScriptLoader.getScripts('x_roho_c2r.clientUtil.jsdbx', function(){
x_roho_c2r.clientUtil.giveAlert();
 alert('testing loop second');

 

Hope this helps. Please mark it as helpful and correct,if applicable.

Thanks and Regards,

Ishita Shrivastava.

Himanshu Dubey
Giga Guru

Hi

Access UI scripts from within client-side code.

There is no constructor for this class. Access methods using the g_ui_scripts global object in any client-side code, such as client or validation scripts.

If calling a UI script with UI Type set to Mobile / Service Portal, use the g_ui_scripts['nameOfScript']; syntax. If calling a UI script with the UI Type set to All or Desktop, use the getUIScript() method to load the script. However, this method is not supported in Internet Explorer 11 when called outside of the Angular application environment. If calling a UI script outside of an Angular context using IE11, you must call the script directly.

Use the then() function to perform an asynchronous action after the call resolves.

Please refer below code to understand how to call UI script in client side scripting

function onLoad() {
    //Call the UI script directly If the UI Type is Mobile / Service Portal, for example:
    //g_ui_scripts['myUIScript'];

    //Use the method if the UI Type is All or Desktop
    g_ui_scripts.getUIScript('myUIScript').then(function(script) {
        script.myUIScriptMethod();
    }, function() {
        console.log('The script did not load');
    });
}
 
Mark Correct if my answer solves your issue and also mark 👍 Helpful if you find my response worthy.

Thanks & Regards

Himanshu Dubey