0% found this document useful (0 votes)
184 views

3.client Script

The document discusses different types of client-side scripts in ServiceNow including onLoad, onSubmit, onChange, and onCellEdit scripts. It provides examples of common use cases for each type of script such as displaying messages or populating fields when a form loads, validating required fields when a form is submitted, and updating dependent fields when a field value changes. The document also covers the main APIs and methods available for client-side scripting in ServiceNow like the G_FORM, G_USER, and GLIDEAJAX APIs.

Uploaded by

Prem Kumar Vula
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views

3.client Script

The document discusses different types of client-side scripts in ServiceNow including onLoad, onSubmit, onChange, and onCellEdit scripts. It provides examples of common use cases for each type of script such as displaying messages or populating fields when a form loads, validating required fields when a form is submitted, and updating dependent fields when a field value changes. The document also covers the main APIs and methods available for client-side scripting in ServiceNow like the G_FORM, G_USER, and GLIDEAJAX APIs.

Uploaded by

Prem Kumar Vula
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

ServiceNow

ClientScripts
◦ What is client-side development?
◦ Client-side development, sometimes referred to as front-end development, is a type of development that involves
programs that run on a client's or user's device. Client-side developers focus on creating the part of a website with
which the user interacts.
◦ This type of development focuses on the front part of an application that users can see. Client-side developers
complete a variety of tasks, including:
 Creating website layouts
 Designing user interfaces
 Adding form validation
 Reviewing the performance of websites
 Adding visual design elements like colors and fonts
 Making website features more functional
 Resolving any issues that users encounter on a site
 Client-side Scripting:
 Client script is a piece of JavaScript code that runs on your web browser instead of the server, thus returning an immediate response.
Using the Client Script, you can perform event-triggered UI actions on the client-side.
 Runs the client side script when event occurs on the browser like loading of form, form submission, change of a field value on the
form
 Client-side scripts execute within a user's browser and are used to manage forms and form fields. 
 For example, when a user fills the email address of a Lead/Contact, you can perform email discovery and automatically populate
relevant data in the form.
◦ Runs the client side script when event occurs on the browser like loading of form,or form submission,change of a field value on the
form.
◦ The Table of client script is sys_script_client.
◦ Where all client script records are stored in servicenow system you can use client script to complete the tasks like i.e validating forms
befor submission setting a field value when another field value is updated with new value on the form.

◦ Client-side Scripting Objectives:


 Describe the purpose of a client-side script and give examples of what client-side scripts can do
 Create, test, and debug Client Scripts
 Create, test, and debug UI Policy scripts
 Use the GlideForm and GlideUser APIs in scripts
 Determine whether to use UI Policy scripts or Client Scripts.
◦ Use client scripts to configure forms, form fields, and field values while the user is using the form. Client scripts
can:
• make fields hidden or visible
• make fields read only or writable
• make fields optional or mandatory based on the user's role
• set the value in one field based on the value in other fields
• modify the options in a choice list based on a user's role
• display messages based on a value in a field
◦ Where client scripts run:
• With the exception of onCellEdit() client scripts, client scripts only apply to forms and search pages. If you create a
client script to control field values on a form, you must use one of these other methods to control field values
when on a list.Create an access control to restrict who can edit field values.
• Create a business rule to validate content.
• Create a data policy to validate content.
• Create an onCellEdit() client script to validate content.
• Disable list editing for the table.
Fields in client script Form:
◦ Active checkbox: To make this client script Enable or Disable.
◦ Inherited: Indicates whether the client script applies to extended tables.
Ex: If you created a client script on task table and you want to run the same client script for incident or problem table
then you can check with this checkbox.
◦ Global: Which you can select to run clientScript for all type of views of the form.
◦ Ex: If you have Multiple views on a form if you want to run same client script on different views all the views you
have then you can make that client script as Global Client.
◦ “Isolating a script" :generally refers to separating a block of code or function from the main script so that it can be
executed independently or reused in multiple places. This can be done for a variety of reasons, including:
1.Improving code organization and readability:
2.Enabling code reuse:
3.Facilitating troubleshooting:
NOTE:
◦ Unlike Business rule Clientscripts can not be configured by selecting values to perform any action.
◦ In business rule even if you don’t write script you can still achieve some functionality with the help of configuration.
◦ In client script you have to write a script in javascript to perform some action in servicenow system.
◦ Mostly Used Methods:
G_FORM:
G_formThe GlideForm API is exposed within G_USER: GLIDEAJAX:
client-side scripts as the variable g_form . This API
provides methods for interacting with forms and
fields, adding messages for the user, adding and
removing options from a drop-down field, and more.

ServiceNow provides a lot of API and methods which you can use on client side code.To perform some
action in client and major API’s we have above three which you can use to achieve different kind of
functionalities.
• Place the cursor in a form field on form load
• Generate alerts, confirmations, and messages
• Populate a form field in response to another field's value
• Highlight a form field
• Validate form data
• Modify choice list options
Types of client script:
• OnLoad
• OnSubmit
• OnChange
• OnCellEdit
• OnLoad: On Load Client Script Runs when user opens the form and before user can enter the data on the
form. It is usually used to set default values on the form or showing message or popup while form is opened.
• If you want to take some action on the form when form is being is loaded.then you can use onload client
script.

Major Use Cases of OnLoad Client Script:


o Showing P1 Message Alert if the priority of current incident is p1.
o Setting logged user in the caller field on incident form.
o Showing message at the top of the form if the caller is VIP user.
◦ Create client script with a message on a Problem form When it is loaded?

◦ hgx
SCENERIO 1:
When P1 incident opened then there should be an alert which says “This incident is P1 Incident”?
O/P:
◦ SCENERIO 2:
◦ When New incident form is opened then caller should be an Populated Automatically with current logged In user?
O/P: caller will Automatically Populated.
◦ OnSubmit: OnSubmit Client Script Runs when form is submitted. It is usually used to validate things on the form before the
submission on the form

◦ Major Use Cases of OnSubmit Client Script:


◦ Showing alert before creation of P1 Incident
◦ Stop user for creating the incident if Assignment group is not correct as per selected Category.
◦ Validate for Mandatory fields on different forms.

◦ Scenerio1:When p1 incident is created then it should ask user to confirm if p1 incident should be
created?


Scenerio2:When network is assignment group category is hardware user should not be able to create incident.
In navigation bar search sys_user_group .LIST Search Network copy sys ID
SCRIPTING : OUTPUT:
Im not able to submit when I try to
submit the incident form
◦ OnSubmit client script is used to perform client-side validation of data before it is submitted to the server.
◦ When a user submits a form in ServiceNow, the OnSubmit client script can be used to validate the data entered in the
form fields. The script can be used to check for mandatory fields, validate input in certain fields, and enforce business
rules. If the validation fails, the script can prevent the form from being submitted and display an error message to the
user.
◦ Once the OnSubmit client script is created, it will run each time the form is submitted, and the validation logic will be
executed to ensure that the data is valid before it is sent to the server.
◦ OnChange: an OnChange client script is used to perform client-side validation of data when a specific field on a form
is changed.
◦ When a user changes the value of a field on a form in ServiceNow, the OnChange client script can be used to validate
the new value entered in the field. The script can be used to check for mandatory fields, validate input in certain
fields, and enforce business rules. If the validation fails, the script can display an error message to the user.
◦ Once the OnChange client script is created, it will run each time the specified field on the form is changed, and the validation
logic will be executed to ensure that the new value entered in the field is valid. If the validation fails, the script can display
an error message to the user.
◦ OnChange: OnChange Client Script Runs when Particular field value changes on the form.it is used when you want to perform some
action on client when field value changes.
◦ OnChange CS Parameters: Function onChange (control,oldValue,newValue,isLoading,isTemplate)

◦ Control: Which stores the dhtml value of the changed field.


◦ Oldvalue: The field when form is opened.
◦ NewValue: which stores the newValue of the field which was changed by the user then it is loading which basically
confirms if the change was done as part of form load.
◦ IsTemplate: it is confirm if change was done as part of template applied on the form.

◦ Major Use Cases of OnSubmit Client Script:


◦ Show an Alert when priority changes to P1
◦ Populate Assignment Group when CI Changes.
◦ Make Assignment group field mandatory when category is hardware.
NOTE: Making field mandatory you can achieve it with the help of UI Policy as well that is the recommended approach but we can also make
fields mandatory read only with the help of ClientScript as well. On that case we can select onchange client script and make one of the field
mandatory
◦ Scenerio1:When state of the incident changes to onhold then worknotes field should become mandatory.
function onChange(control, oldValue, newValue, isLoading) {
This is the function declaration for a client-side script that executes when the "State" field is changed.
The parameters "control", "oldValue", "newValue", and "isLoading" are passed automatically to the function by the
platform.
kotlin
if (isLoading || newValue === '') { return; }
This block of code checks whether the form is currently loading or if the "State" field has been left blank. If either of
these conditions
is true, the function exits early and does not execute the rest of the code.
◦ OnCell Edit: On cell edit is a client-side event that occurs when the user edits the contents of a cell in a table or a grid.
◦ When a user edits the contents of a cell in a list or form in ServiceNow, the On cell edit event can be used to trigger client-
side scripts that can perform various actions, such as validation of user input, updating other cells in the same record or other
records in the list or form, or sending the edited data to the server.
◦ To implement On cell edit event in ServiceNow, you can use the GlideList2ClientSideEdit class and create a client script that
implements the OnCellEdit function. Here is an example of how to implement On cell edit event in a list view in ServiceNow:
◦ function OnCellEdit(table, row, cell, oldValue, newValue, isDerived) {
◦ // Your code here
◦ }
◦ Once the On cell edit event is implemented, the client script will be triggered each time the user edits the contents of a cell
in the list view, and the OnCellEdit function will be called with the table, row, cell, oldValue, newValue, and isDerived
parameters that represent the edited cell and its new value.
◦ You can use these parameters to perform various actions on the edited data, such as updating other fields in the same record
or other records in the list view.
what is g_form?
g_form is a global JavaScript variable in ServiceNow that provides a set of methods to interact with a form displayed on a web
page. These methods can be used in client scripts to access and manipulate form fields
and values, as well as to perform various actions on the form, such as submitting or saving it.
Some common methods available in the g_form object include:
•getValue(): Gets the current value of a specified form field.
•setValue(): Sets the value of a specified form field.
•getControl(): Retrieves a reference to a specified form field element.
•addInfoMessage(): Adds an informational message to the form.
•addErrorMessage(): Adds an error message to the form.
•save(): Saves the current form without closing it.
•submit(): Submits the current form.
These methods can be used to create dynamic and interactive forms in ServiceNow, and can
be combined with other features such as UI policies and business rules to further customize the behavior of forms based
on user actions and input.
◦ What is client-side API and server-side API in Service now?
◦ The client-side API consists of JavaScript functions that are executed on the client-side of a ServiceNow instance, typically
within the web browser of a user accessing the instance. These functions are used to interact with the user interface and
manipulate data displayed on the screen. Examples of client-side APIs in ServiceNow include the g_form API for working with
form data, and the g_user API for retrieving user information.
◦ The server-side API, on the other hand, consists of server-side JavaScript functions that are executed on the ServiceNow
server. These functions are typically used in business rules, scripts, and other server-side logic to manipulate data in the
ServiceNow database. Examples of server-side APIs in ServiceNow include the gs API for accessing system properties and logs,
and the GlideRecord API for querying and manipulating records in the database.

You might also like