Client Side Scripting ServiceNow
Client Side Scripting ServiceNow
ServiceNow
An Overview of Client Scripts and
Their Functionality
Introduction
• Client-side scripting in ServiceNow allows real-
time interaction with form fields, enhancing
user experience and enforcing business logic.
What is a Client Script?
• A Client Script is a piece of JavaScript code
that runs on the client side (browser) to
manipulate form fields, validate inputs, and
trigger actions.
Types of Client Scripts
• - onLoad: Executes when a form is loaded
• - onChange: Runs when a field value changes
• - onSubmit: Validates before form submission
• - onCellEdit: Triggers when a cell value is
edited in a list
GlideForm (g_form)
• g_form is used to interact with form fields in
ServiceNow.
• Common Methods:
• - g_form.getValue('field_name')
• - g_form.setValue('field_name', 'value')
• - g_form.addErrorMessage('Error message')
GlideUser (g_user)
• g_user provides information about the logged-
in user.
• Common Methods:
• - g_user.hasRole('role_name')
• - g_user.userID
• - g_user.userName
GlideAjax
• GlideAjax is used to call server-side scripts
from the client-side.
• Example:
• var ga = new GlideAjax('ScriptIncludeName');
• ga.addParam('sysparm_name',
'methodName');
• ga.getXMLAnswer(callbackFunction);
Best Practices
• - Use minimal client scripts to improve
performance.
• - Avoid synchronous GlideAjax calls.
• - Reuse Script Includes for server-side logic.
• - Test scripts thoroughly before deploying.