🔍 Client Scripts
What Are Client Scripts?
Client Scripts run in the browser (client-side) and enhance user interaction with real-time form behavior. They improve Form UX without needing a round-trip to the server. Whether you're auto-filling fields, validating inputs, or hiding/showing data. Client Scripts make ServiceNow forms Intelligent and Responsive.
🧰Types of Client Scripts
(With Examples):
1) onLoad– //👉 Executes when a form loads Use Case: Auto-populate default values or show a message on load.
2) onChange – //👉 Triggers when a field value changes Use Case: Show/hide fields or validate related data live.
3) onSubmit– //👉 Runs before form is submitted Use Case: Prevent invalid submissions or display alerts.
4) onCellEdit– //👉 For list editing (since UI16+) Use Case: Validate or auto-update related fields in list view.
Why Client Scripts Are So Useful?
Improve user experience Reduce data-entry errors Avoid unnecessary server calls Build intuitive forms without reloads Best Practices to Follow: Use g_form object effectively Avoid hardcoding – use getReference() where needed Use return false; wisely in onSubmit Test for different roles/views Always check performance impact.
🧠 Top 10 Interview Questions & Answers
1️⃣1)What is a Client Script in ServiceNow?
JavaScript code that runs on the client-side to enhance form behavior.
2️⃣ Name the types of Client Scripts?
onLoad, onChange, onSubmit, onCellEdit.
3️⃣ What is the g_form
object?
The API to interact with form fields, visibility, and values.
4️⃣How do you prevent a form from submitting?
Use return false;
in an onSubmit script.
5️⃣ Difference betweeng_form.getValue()
and g_form.setValue()
?
Get vs. set field values on the form.
6️⃣ Can you write more than one script of the same type?
Yes, but prioritize logic flow and avoid conflicts.
7️⃣ How to check if a field is mandatory in client script?
g_form.setMandatory('field_name', true);
8️⃣ How do you access a reference field’s display value?
Use getReference()
with a callback.
9️⃣ How can you debug a client script?
Use browser console.log()
or debugger tools.
🔟How do you ensure a script only runs for certain users?
Use conditions or check roles within the script.