ServiceNow Client Scripts: A Comprehensive Developer's Guide


Introduction
ServiceNow is a powerful platform for digital workflows, and at the heart of its dynamic user experience are Client Scripts. These small but mighty pieces of JavaScript run directly in the user’s browser, allowing real-time form manipulation, validation, and interaction without needing server round-trips.
In this guide, we’ll dive deep into what Client Scripts are, explore their types, show real-world examples, and share best practices to help you master them as a ServiceNow Developer.
What are Client Scripts?
Client Scripts are JavaScript code blocks that execute in the browser (client-side) when forms or lists are loaded, changed, or submitted. They are used to:
Validate data before it reaches the server
Manipulate form fields (show/hide, read-only, mandatory, set values)
Enhance user experience through real-time interactions
Perform calculations based on user inputs
Unlike Business Rules, which run on the server, Client Scripts offer immediate feedback to the user, improving UI responsiveness.
Types of Client Scripts in ServiceNow
ServiceNow supports four types of Client Scripts, each triggered by specific events.
1. onLoad() – Run when the form loads
Purpose: Set initial values, show/hide sections, make fields read-only, or display messages.
function onLoad() {
if (g_form.isNewRecord()) {
g_form.setMandatory('short_description', true);
g_form.showFieldMsg('short_description', 'Please provide a short description.', 'info');
}
}
2. onChange() – Run when a field value changes
Purpose: Populate fields dynamically, show/hide fields based on selection, or perform live validations.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;
if (g_form.getValue('category') === 'hardware') {
g_form.setMandatory('subcategory', true);
} else {
g_form.setMandatory('subcategory', false);
}
}
3. onSubmit() – Run when the form is submitted
Purpose: Final client-side validation before the form is submitted to the server.
function onSubmit() {
var state = g_form.getValue('state');
var assignedTo = g_form.getValue('assigned_to');
if (state === '6' && assignedTo === '') {
g_form.showErrorBox('assigned_to', 'Please assign the incident before resolving it.');
return false;
}
return true;
}
4. onCellEdit() – Run when editing list fields (in-line)
Purpose: Validate or restrict inline edits in list views.
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
for (var i = 0; i < sysIDs.length; i++) {
var oldValue = oldValues[i];
if (newValue === 'false' && oldValue === 'true') {
g_form.addInfoMessage('Cannot deactivate directly from list. Please open the form.');
saveAndClose = false;
}
}
callback(saveAndClose);
}
Note: OnCellEdit scripts are more complex. Use
GlideAjax
if you need server-side data (e.g., record state).
Best Practices for Client Scripts
To ensure performance, maintainability, and scalability, follow these best practices:
Practice | Recommendation |
Keep it simple | Don’t overload client scripts with too much logic. Offload to Script Includes or Business Rules if needed. |
Use ServiceNow APIs | Use g_form , g_user , and GlideAjax . Avoid GlideRecord on client-side. |
Avoid direct DOM manipulation | Use g_form methods instead of native JS to show/hide/modify fields. |
Add clear comments | Document what your script is doing and why. Future you (or your team) will thank you. |
Test in all roles and browsers | Ensure consistent behavior for different user roles and on major browsers. |
Use UI Policies when possible | For simple field behaviors (mandatory, read-only), prefer UI Policies over scripts. |
Watch performance | Avoid chaining too many scripts that slow form loading. |
Final Thoughts
Client Scripts are essential to creating intelligent, responsive, and user-friendly forms in ServiceNow. When used correctly—with a balance of performance and functionality—they can significantly improve the overall user experience.
Remember:
Use the right type of Client Script for your use case
Keep logic efficient and well-documented
Combine with
GlideAjax
,UI Policies
, andScript Includes
for optimal results
Pro Tip: Want to perform server-side validation or fetch records? Use GlideAjax with a Script Include. Never use GlideRecord
directly in a Client Script!
Happy scripting🥰!
Subscribe to my newsletter
Read articles from Anna Sivasankar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
