- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
02-14-2024 11:23 PM - edited 12-02-2024 08:40 PM
Hello Community,
Hope you are doing well!
Today I am going to share some client script & their types with some example. Hope it will helpful to you!!
1. Client Scripts :
-------------------------
In ServiceNow, client scripts are scripts that run on the client-side, meaning they execute within the user's browser when certain actions occur on a form or a UI page. These scripts are written in JavaScript and are used to enhance user experience, perform client-side form validation, manipulate fields, and interact with the user interface.
Here's a basic example of a client script in ServiceNow:
To create a client script in ServiceNow:
Navigate to System Definition > Client Scripts.
Click on New to create a new client script record.
Provide a Name and Description for your client script.
Write your JavaScript code in the Script field.
Save the record.
Once the client script is saved, it will automatically run on the client-side whenever the specified conditions are met.
Types of Client Scripts :
-------------------------------
In ServiceNow, there are several types of client scripts that you can use to enhance the behavior and functionality of forms, UI pages, and UI actions. Here are the main types of client scripts in ServiceNow along with examples for each:
1. OnLoad Client Scripts:
These scripts execute when a form or UI page is initially loaded. They are used to perform actions when a form is opened.
Example:
// Set focus to the short_description field when incident form loads
g_form.getControl('short_description').focus();
2. OnChange Client Scripts:
These scripts execute when a field value changes. They are commonly used for field validation, visibility toggling, or dynamically populating other fields.
Example:
// If priority is set to High, set the state to In Progress
if (newValue == 1) {
g_form.setValue('state', '2'); // In Progress
}
3. OnSubmit Client Scripts:
These scripts execute when a form is submitted, typically used for final form validation or triggering specific actions before submission.
Example:
// Validate the form before submission
function onSubmit() {
if (!g_form.getValue('short_description')) {
alert('Please provide a short description.');
return false; // Prevent form submission
}
return true; // Allow form submission
}
4. Catalog Client Scripts:
These scripts specifically apply to catalog items and are used to customize user interactions within the Service Catalog.
Example:
// Auto-populate a date field with today's date
var today = new Date();
g_form.setValue('expected_start', today);
5. UI Policy Client Scripts:
UI Policies are used to dynamically change the behavior of form elements. Client scripts attached to UI Policies run when the conditions specified in the UI Policy are met.
Example:
// Hide the due date field when the state is Closed
if (newValue == '6') { // Closed
g_form.setDisplay('due_date', false);
} else {
g_form.setDisplay('due_date', true);
}
6. UI Action Client Scripts:
These scripts are used to control the behavior of UI actions, such as buttons or context menu items.
Example:
// Execute custom logic when a UI action button is clicked
function doSomething() {
alert('UI action button clicked!');
}
7. Client Scripts in Script Includes:
Scripts included in Script Includes can also be used as client scripts to provide reusable logic across multiple client scripts.
Example:
// Shared function to display a confirmation dialog
function showConfirmation(message) {
if (confirm(message)) {
return true;
} else {
return false;
}
}
These are some of the main types of client scripts in ServiceNow along with examples demonstrating their usage.
For more information please visit the link below:
https://developer.servicenow.com/dev.do#!/learn/courses/utah/app_store_learnv2_scripting_utah_script...
That's all from my side.
If you all like it or it is helpful for you, Please mark it as helpful or bookmark this article for future refence.
Regards,
Chaitali.
- 15,579 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks chaitali
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
i want to add a plus/add button near to strung field on the Form and i dont know how to do it . i dont want the UI Action at the top of the form.
Any help will be appreciated. Thanks in advance.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
hi,
In order to add a plus/add button near to string field on the Form , use UI macro.
please find the link below for reference.
Examples of UI Macros: UI macros Archives - ServiceNow Guru
You can use this link to achieve the result.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I would write client script onLoad type but it doesn't execute in incident from. What is the reason.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I believe, we cannot use 'current' object in client script.
You have mentioned 'current' object in the example of onChange client script.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Client Script APIs
I would like to add the list of Client Side APIs:
1) Glide User
2) Glide Form
3) Glide Ajax
4) Glide Modal form
5) Glide URL
6) Glide Flow
7) Glide List
From the above list 2 client side APIs are widely used : Glide User and Glide Form.
If my content was helpful, please mark my answer helpful 👍 so it can be valuable to others.
Thanks
Jaishree R
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
More Client Script Use cases/ Requirements to Practice:
Requirement 1: If the current logged in user has ITIL role , then they should be able to see 2 sections 1) Resolution Information 2) System Information
Script : we have to write onload client script
if(g_user.hasRoleExactly('itil'))
{
g_form.setsectionDisplay('resolution_information’, true);
g_form.setsectionDisplay(‘system_information', true);
}
else
{
g_form.setsectionDisplay('resolution_information’, false);
g_form.setsectionDisplay(‘system_information', false);
}
Thanks
Jaishree R