Is there a way to check if a reference field gets cleared in an OnChange Client Script

Eric Woody
Tera Expert

Basically what the subject says, I need a way to see if a reference field is cleared (user just highlights and deletes the content). OnChange doesn't capture it even removing the newValue == '' default at the top of it. I'm guessing because it's technically not cleared and the value hasn't really changed. Is there a way around this? I'm aware I can do this in a Business Rule, but I really prefer not to. 

 

Generic example of what I'm doing if needed:

 

 

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

if(newValue == ''){
g_form.addErrorMessage('My Error Message');
g_form.setValue('ref_field', oldValue);
}
//Type appropriate comment here, and begin script below
}

 

 

 

1 ACCEPTED SOLUTION

Eric Woody
Tera Expert

I figured out the issue. I was having a brain fart I guess. Just needed to check the inherited checkbox as where the script was running was on a child table. (facepalm)

 

Thanks everyone.

View solution in original post

5 REPLIES 5

AshishKM
Kilo Patron
Kilo Patron

Hi @Eric Woody 

I think there is missing "}" with if condition and if there is no change means there is no data in newValue object and you don't need to set the variable with oldValue object.

add the alert(newValue) and check if there is some value.

 

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

That missing { was just me removing out any sensitive information, my code has it. I do an alert before the if statement and it never fires, like nothing has changed, even though I have cleared the field and clicked off of it.

can you share more details on this form and field name where you are applying this onChange client script. Just re-check if correct Field Name configured in client script

AshishKMishra_0-1704316977265.png

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Michael Jones -
Giga Sage

Typically speaking an onchange client script (after you remove the default condition check for blank) will react to the field being blanked out as long as it actually had a value - meaning the user made a selection, or typed a value and actually exited the field. It won't fire on a string field, for example, if they type something but then delete it before they exit. 

 

Try something like this and see what results you get. 

Might go without saying, but make sure you have the correct field selected (that's caught me a few times). 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading ) {
      return;
   }
	alert("New Value: " + newValue + " Old Value: " + oldValue + " type is: " + typeof(newValue));
}

 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!