onChange Client script ignores empty field

Derek Behunin
Tera Guru

Hey guys,

I'm working on a client script that clearValue()'s fields below a given field on a form. I'm using an onChange script but have an edge case thats giving me some unexpected behavior. The targeted field is a reference field, but if the user manually clear's the field the onChange script does not run. If the field is then given value either through a reference or user input the onChange script runs as expected. The primary issue here is that a user can accidentally clear the field, then put the original value of the field back but sub fields get cleared even though technically the field "hasn't" changed.

Is there any way to check if the reference field has been cleared by the user?

1 ACCEPTED SOLUTION

Elijah Aromola
Mega Sage

The default script contains a line that checks for newVal == ''" and if so exit the script. You can remove that from if statement in the script and it will run whatever code you have.

 find_real_file.png

View solution in original post

2 REPLIES 2

Elijah Aromola
Mega Sage

The default script contains a line that checks for newVal == ''" and if so exit the script. You can remove that from if statement in the script and it will run whatever code you have.

 find_real_file.png

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Derek,

when you use clearValue() it doesn't trigger the onChange script

try removing newValue === '' in the function

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

change this to below

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

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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