Give me couple of examples for each type of client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 03:23 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 03:35 AM
Hello @Subhashini C
Do check this videos:
https://youtu.be/AqUY98OToGY?si=07P-yPh3Azogi5b0
https://youtu.be/7QF0C6D9Cr0?si=Fy3cDRKh3y-OUuMj
https://youtu.be/lla0rWXuInA?si=DBLArt1JKaKbwjpA
https://youtu.be/iAmrt-75sBA?si=_-XuNx4oQ-4oimPM
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 03:40 AM
There are usually 4 types of client scripts in ServiceNow.
1.onLoad Client Scripts: These scripts run when a form loads.
function onLoad() {
// Get the GlideForm object
var gf = g_form;
// Make the 'comments' field read-only
gf.setReadOnly('comments', true);
// Make the 'short_description' field read-only ONLY on new records
if (g_form.isNewRecord()) {
gf.setReadOnly('short_description', true);
}
}
2. onChange Client Scripts: These scripts run when a specific field's value changes.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return; // Don't run on initial load or if the field is cleared
}
if (newValue == 'problem') {
g_form.setVisible('problem_details', true); // Show the 'problem_details' field
} else {
g_form.setVisible('problem_details', false); // Hide the 'problem_details' field
}
}
3. onSubmit Client Scripts: These scripts run when a form is submitted. They are crucial for data validation.
function onSubmit() {
if (g_form.getValue('short_description') == '') {
alert('Short description is required!');
return false; // Prevent submission
}
}
4. onCellEdit Client Scripts: These scripts run when a cell in a list is edited (inline editing).
function onCellEdit(sys_id, table, old_value, new_value, is_loading) {
if (is_loading) {
return;
}
if (isNaN(new_value)) {
alert('Please enter a number.');
g_form.setCellDisplayValue(table, sys_id, 'quantity', old_value); // Revert to the old value
return false; // Prevent the change
}
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 03:40 AM
Hello @Subhashini C ,
The below thread may help you to understand more about client scripts.
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Thanks,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 03:41 AM
I will suggest you to go through docs, youtube videos.
I will also suggest to take platform fundamentals course from now learning so that you get hands-on experience.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader