How to execute a scoped application UI script (not Global script) from UI policy run script,

mkm1
Mega Guru

I have a need to execute a scoped application UI script (not Global script) from UI policy run script, I have various options, but it won't execute. Below is a very simple sample script that I am trying to execute from UI policy run script and UI Script. Any guidance will help.

 

mkm1_0-1685479375392.png

 

mkm1_1-1685479375413.png

 

 

7 REPLIES 7

Gotcha...sorry I couldn't be of more help! Glad to know its a specific problem, likely someone has a good answer -- btw, I saw this on your screenshots from a different post in the forum (the screenshots here are lowrez blurry, fyi)...for what it's worth, I noticed that it seems like you're passing the script's function in as a parameter to the call of the same script...not sure if that will work (but there's a lot I don't know 😉 )

Having said that, 

 

UI scripts (servicenow.com) wrote:

 

Call a UI script from client-side code

Access UI scripts from within client-side code using the g_ui_scripts global object. For more information, see GlideUIScripts - Client.

Note: This class does not support UI scripts with the Global field set to true.

&&

 

GlideUIScripts - Client (servicenow.com) wrote:

 

function onLoad() {
    g_ui_scripts.getUIScript('myUIScript').then(function(script) {
        script.myUIScriptMethod();
    }, function() {
        console.log('The script did not load');
    });
}

...perhaps the "ScriptLoader" method is not available outside of global scope...?

AmishRanjan2
Mega Guru

Hi @mkm1 ,

If both ui script and ui policy are created in same application, then directly use the API name of the ui script which will be there in the ui script record in "API Name" field in the ui policy script and then can use dot and then name of the method to access the required methods specified in the ui script.

 

Example: sn_compliance.PolicyExceptionUtils.validateExceptionDuration("valid_from", "requested_valid_to", "requested_valid_to");

 

Screenshot 2023-05-31 at 11.58.37 AM.png

Best Regards,
Amish Ranjan

Thanks for your suggestion. I tried calling it directly from UI policy script onCondition as shown below, it didn't work.

x_20949_ppt.ShowRelatedList.displayRelatedList('procurement_action', 'x_20949_ppt_awards_rejected');

 

I copied the UI script as it's into UI policy it works perfectly. But that's not I wanted; I want to reuse the UI script anywhere just calling the function with arguments.