Call UI Script From Client Script | Practical Demonstration

You will get the information in this article such as 1) What is UI Script? 2) Step to Step representation of implementing UI Script, Client Script and representation that How we can call UI Script from Client Script? 3) Video Demonstration of implementing the use case.

What is UI Script?

We can say that UI script is a reusable global client-side java script code which we can call or run from other client-side scripts. Such as Client Scripts, UI Pages, UI Macros, HTML Code etc.

Or, in other words UI Script is a package which store client-side JavaScript that we can call from any other client-side scripts.

How to call UI Script from Client Script?

Below is the use case or a scenario example to provide complete understanding that how we can create UI script and how we can call it from Client Script.

Simple Use Case Scenarios:

When user create any record such as incident, problem etc. Then the message should display on the top that how many total numbers of records has been created by him of the same.

UI Script Code: 

We will create the class and methods in UI script and these class and methods are reusable, means we can initialize class and call methods anywhere in the client side scripts to achieve the required output. 

Below are the steps to Create UI Script in ServiceNow:


1. Login as ServiceNow administrator
2. Type "UI Scripts" in application navigator, under "System UI" you will find the UI Scripts module.
3. Click on UI Script and then click on new button.
4. Give API Name for e.g. "Global.UIScriptDemo" //This API Name is very useful when we call UI script in UI pages and in HTML scripts. Try to give API name with prefix application name and then (.)dot and then the Class name decided to create in Script) 
5. Select UI type as Desktop
6. Check global check box (should be checked so that we can call UI Script from Client Script)
7. Enter Description Text
8. Write Script (For understanding purpose, we have provided the script below which we have used in UI Script section video demonstration. Video link is provided below for the reference)

var UIScriptDemo=Class.create();
UIScriptDemo.prototype={
initialize: function()
{
},
demoCallFromClientScript: function(TableName)
{
var count=0;
var objone= new GlideRecord(TableName);
objone.addQuery('sys_created_by',g_user.userName);
objone.query();
while(objone.next())
{
count++;
}
g_form.addInfoMessage("Total Number of "+TableName+" Records Created by you are : "+count);
},
};

Client Script Code: 

As per use case, we want that when page gets load, then the message will display to user with the required count. So, in below script we will create onload client script, where we initialize class created in UI Script and then through Object, we can call the required method defined in initialized class. Below code will provide you the idea that how we can call UI Script methods in Client Scripts. There are other ways to but I mostly use this way to implements the requirement related to UI Scripts.

Steps to Create onLoad Client Script:


1. Type "Client Scripts" in Application Navigator.
2. Click on Client Scripts and then click on new button.
3. Enter name. for e.g. CallUIScript
4. Select table such as "Incident"
5. Select UI Type such as Desktop
6. Select type "On Load" // Because we want to execute the use case on onLoad.
7. Enter Description Text.
8. Write Script (For understanding purpose, we have provided the script below which we have used in Client Script section in video demonstration. Video link is provided below for the reference)

function onLoad() {
   var abc=new UIScriptDemo();  // Initializing the class created in UI Script 
abc.demoCallFromClientScript("kb_knowledge");  // Calling the function created in UI Script
   
}

If above steps are not helping much to understand that how you can call UI Script in client script, you can refer below You tube video link, which will provide practical demonstration of implementing the above steps and use case requirement. Please click the link below:

How to Call UI Script from Client Script Practical Demonstration Video



We believe above information of calling UI Script from Client Script will be helpful for you and help you to implement the required topic. If above article is helpful then please do share it with others and if you have any suggestions then please feel free to comment below. Happy Learning, Basico ServiceNow Learning. Thankyou!!!

1 comment:

Thankyou !!!!

Powered by Blogger.