Sameer18
Tera Contributor

UI scripts provide a way to package client-side JavaScript into a reusable form, similar to how script includes store server-side JavaScript. Developer can create UI scripts and run them from client scripts and other client-side script objects and from HTML code.

Please note UI scripts are not supported for mobile.

---------------------------------------------------------------------------------------------------------

For this use case we will be using UI script to package validation functions such as date validation, email validation, phone number format validation, etc and use them from client scripts.

What is Utility script?
Utility (helper) script, is a script, which contains just a bunch of re-usable related functions, so they can be reused across the application.

Below are the steps:

 

1. Use UI script to write reusable functions:

  • To create UI scripts, navigate to System UI > UI Scripts and create or edit a record.
  • Mark a UI script as Global to make it available on any form in the system
 
FieldDescription
Script NameName of the UI script. Ensure the name is unique on your system.
API NameThe API name of the UI script, including the scope and script name (for example, x_custom_app.HelloWorld).
ApplicationApplication that contains the UI script.
ActiveIndicator of whether the UI script is active. Only active UI scripts can run.
Global

Indicator of whether the script loads on every page in the system.

DescriptionSummary of the purpose of the script.
ScriptClient-side script to run when called from other scripts.

    2. Loading the UI script in Client Script:

  • Create a onLoad client script on a table of your interest
  • Preferably give lower order so that utility functions will be available for all client script
  • Use ScriptLoader API to load the UI script.
function onLoad() {
   // Load the script required to validate the dates
   ScriptLoader.getScripts('UIScriptName.jsdbx', function() {});
}

Reference: "Date Validation Load" OOTB client script on 'change_request' table.


3. Using util functions in Client Script:

  • In your client script use function from Util UI script.
function onSubmit() {
	var showErrorMsg = function(errorMsg){
		g_form.clearMessages();
		g_form.addErrorMessage(errorMsg);
	};
	if (typeof validateStartDateBeforeEndDate === "function")
		return validateStartDateBeforeEndDate("work_start", "work_end", showErrorMsg);
}

Reference: "Actual Start/End Dates Validation" OOTB client script on 'change_request' table.

 

Note: Use caution when creating global UI scripts because they can impact performance. You cannot create a global UI script in a scoped application.

Comments
jraki
Tera Contributor

I'm moving to Agent Workspace and I need to reuse some of my UI scripts to be used in the client scripts at AW side.

I tried to follow the documentation related to GlideUIScripts and to switch the UI Type of my UI scripts from Desktop to All. This did not work and ServiceNow support told me that g_ui_scripts API is intended for Mobile / Service Portal UI Type use only and has been misdocumented and they proposed me to use ScriptLoader that is not supported for mobile. But g_ui_scripts is not working also for mobile.

Is there any way to use my UI scripts at AW side? 

Best Regard

Version history
Last update:
‎12-13-2019 04:02 AM
Updated by: