- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2025 01:43 AM - edited 11-12-2025 01:44 AM
Hi everyone,
I’m getting the message “There is a JavaScript error in your browser console” when my onLoad() client script runs on an HR Task form.
After testing, I found that the issue comes from the part of my script where I hide some fields and use g_form.onChange()
var hideOtherInfoFields = [
"ethnicity",
"religion_section",
"blood_group",
"hobbies",
"languages",
"driving_licence_vse",
"disability_card_number"
];
hideOtherInfoFields.forEach(function (field) {
if (g_form.getControl(field)) {
g_form.setVisible(field, false);
g_form.setDisplay(field, false);
g_form.setMandatory(field, false);
}
});
if (g_form.getControl('disability_card_number')) {
g_form.setVisible("disability_card_number", false);
g_form.setDisplay("disability_card_number", false);
g_form.setMandatory("disability_card_number", false);
}
g_form.onChange('do_you_have_any_disability', function () {
if (g_form.getControl('disability_card_number')) {
g_form.setVisible("disability_card_number", false);
g_form.setDisplay("disability_card_number", false);
g_form.setMandatory("disability_card_number", false);
}
});
When I comment out this section, the error disappears.
Has anyone faced a similar issue?
Any insights would be appreciated!
Thanks in advance 🙏
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @ghitabahaj ,
The error occurs because g_form.onChange() is not a valid function in ServiceNow. You cannot call it inside an onLoad client script. OnLoad scripts are only meant for initializing fields (hiding, showing, setting mandatory, etc.) and cannot attach dynamic change listeners.
If you need to react to field changes, you should use a separate onChange client script for that specific field, or handle it in onLoad using g_form.getValue() to set visibility/mandatory based on the current value.
Thank and regards,
Siddhesh Jadhav
Accept my answer if it solved your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2025 02:29 AM
Hope you are doing good.
Did my reply answer your question?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @ghitabahaj ,
The error occurs because g_form.onChange() is not a valid function in ServiceNow. You cannot call it inside an onLoad client script. OnLoad scripts are only meant for initializing fields (hiding, showing, setting mandatory, etc.) and cannot attach dynamic change listeners.
If you need to react to field changes, you should use a separate onChange client script for that specific field, or handle it in onLoad using g_form.getValue() to set visibility/mandatory based on the current value.
Thank and regards,
Siddhesh Jadhav
Accept my answer if it solved your query.
