Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

GhitaB
Tera Contributor

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 🙏

4 REPLIES 4

Sarthak Kashyap
Mega Sage

Hi @GhitaB ,

 

I think you have to create 2 client script one is onLoad and 2nd is onChange on do_you_have_any_disability this variable and add below code and remove that code from onLoad client script.

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

SharmaJi_SNow
Mega Guru

Hey there, 


I’ve seen similar JavaScript error in the browser console  issues before — they often happen when a script uses a method that isn’t supported in Client Scripts. It might be worth double checking which g_form methods are valid in your instance.

You can review the official GlideForm API reference here:

🔗 https://developer.servicenow.com/dev.do#!/reference/api/rome/client/c_GlideFormAPI

If possible, could you share a screenshot of the exact console error message? That would help pinpoint which line or method is causing it.

Sometimes even a small mismatch in API usage can trigger console errors like that.

Hope this helps point you in the right direction!

Nawal Singh
Tera Guru

Hi @GhitaB ,

your code- 

g_form.onChange('do_you_have_any_disability', function () { ... }); is creating an issue

 

Use a separate onChange client script-

Create a new Client Script:

- Type: onChange

- Field name: do_you_have_any_disability

- Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;

    var showDisability = newValue === 'yes'; // adjust to your choice value
    g_form.setDisplay('disability_card_number', showDisability);
    g_form.setMandatory('disability_card_number', showDisability);
}

 

for hide/show and mandatory/non-mandatory you can also use UI Policy.

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Ankur Bawiskar
Tera Patron
Tera Patron

@GhitaB 

you are correct.

the error is due to that g_form.onChange() line.

I don't think it's a valid function and hence throwing eror

You need to write separate onchange script on that field and then handle the logic there

💡 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