JavaScript error in onLoad client script when hiding fields and using g_form.onChange()

ghitabahaj
Tera Expert

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 🙏

1 ACCEPTED SOLUTION

Siddhesh Jadhav
Kilo Sage

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.

View solution in original post

7 REPLIES 7

@ghitabahaj 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@ghitabahaj 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Siddhesh Jadhav
Kilo Sage

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.