Client Scripts
💻 Client Scripts in ServiceNow
🌐 1. Introduction
Client Scripts are pieces of JavaScript that run in the user’s browser (client-side) when interacting with forms and fields in ServiceNow.
They are used to:
-
Control form behavior dynamically
-
Validate field values before submission
-
Improve the user experience (UX) by reducing server calls
📑 2. Types of Client Scripts
-
onLoad()
-
Runs when a form loads.
-
Example: Hide the “VIP” checkbox for non-admins.
-
-
onChange()
-
Runs when a field value changes.
-
Example: If Priority = 1, auto-populate Impact = High.
-
-
onSubmit()
-
Runs when the user clicks Submit/Update.
-
Can validate or block submission.
-
Example: Prevent form submission if “Short Description” is empty.
-
-
onCellEdit()
-
Runs when a list field is edited inline.
-
Example: Validate that SLA date is not set in the past when editing list rows.
-
⚡ 3. Client Script Examples
Example 1: onLoad Script
function onLoad() {
if (g_user.hasRole('itil') == false) {
g_form.setVisible('vip', false); // Hide VIP field for non-ITIL users
}
}
Example 2: onChange Script
function onChange(control, oldValue, newValue) {
if (newValue == '1') {
g_form.setValue('impact', '1'); // High impact if Priority = Critical
}
}
Example 3: onSubmit Script
function onSubmit() {
if (g_form.getValue('short_description') == '') {
alert("Short Description is mandatory!");
return false; // Prevent submission
}
return true;
}
Example 4: onCellEdit Script
function onCellEdit(sysIDs, table, oldValues, newValue) {
if (newValue < gs.nowDateTime()) {
alert("SLA Date cannot be set in the past!");
return false;
}
return true;
}
🔍 Advanced Concepts
-
g_form API
-
Used to manipulate fields in forms (setValue, setVisible, setMandatory, etc.).
-
-
g_user API
-
Used to check current user roles, groups, or properties.
-
-
Client Callable Script Includes
-
Used when Client Script needs server-side data without refreshing the page.
-
Called using
GlideAjax
.
var ga = new GlideAjax('UserInfo'); ga.addParam('sysparm_name', 'getManager'); ga.addParam('sysparm_user', g_form.getValue('caller_id')); ga.getXMLAnswer(function(response) { g_form.setValue('manager', response); });
-
-
Best with UI Policies
-
Use UI Policies whenever possible (simpler and faster).
-
Use Client Scripts only when advanced logic is required.
-
🛠️ Real-World Use Cases
-
Incident Form: Auto-populate Impact and Urgency based on Priority.
-
Change Request: Prevent submission unless Risk is assessed.
-
HR Case Form: Hide sensitive fields unless the user is in the HR group.
-
Service Catalog Item: Auto-calculate cost when quantity changes.
-
CMDB Form: Validate serial numbers follow a specific pattern.
💡 Best Practices
-
✅ Use UI Policies first—they are simpler and more efficient.
-
✅ Write modular scripts—avoid hardcoding values.
-
✅ Document Client Scripts for future maintenance.
-
✅ Use GlideAjax instead of
g_form.submit()
for server calls. -
✅ Test in sub-production environments before going live.
-
❌ Avoid excessive Client Scripts—they slow down form performance.
-
❌ Don’t duplicate logic between Client Scripts and Business Rules.
🎬 Conclusion
Client Scripts in ServiceNow make forms dynamic and interactive, improving the end-user experience.
-
onLoad, onChange, onSubmit, and onCellEdit scripts provide flexibility for different use cases.
-
Combined with g_form, g_user, and GlideAjax, Client Scripts can deliver powerful, real-time validation and automation.
Comments
No comments yet.