Check if oldValue was empty/null in onChange client script?

Dbarre
Tera Contributor

How can I check if the oldValue is empty or null? I have a client script that clears values of multiple fields, but I only want it to clear those fields IF the oldValue of the field wasn't empty and actually held a value.

So for instance, I have three fields:

Field AReference
Field BReference
Field CReference

 

When 'Field A' changes value, it will clear 'Field B' and 'Field C'. I only want this to clear values though IF Field A wasn't empty previously. So if I Populate Field C and Field B before populating Field A, I don't want it to clear those fields because Field A didn't have a value previously.

I've tried the following with no luck:

if (!oldValue) {
		g_form.clearValue('Field_B');
		g_form.clearValue('Field_C');
	}
if (oldValue != null) {
		g_form.clearValue('Field_B');
		g_form.clearValue('Field_C');
	}
if (oldValue != '') {
		g_form.clearValue('Field_B');
		g_form.clearValue('Field_C');
	}

Nothing seems to work. I only want it to clear those values if there was a value in 'Field A' previously.

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

This script works onChange of Field_A, provided the field names (not labels) of Field_B and Field_C are correct (case-sensitive).  Keep in mind that oldValue will always be the value of Field_A when the form loads.  So if Field_A is populated when the record loads, then you blank it, then populate B and C, then populate A, B & C will still get cleared.  If you are populating Field_A with a default value or onLoad script, you'll have to test with an alert to see what oldValue is.

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

   if(oldValue != ''){
	g_form.clearValue('Field_B');
	g_form.clearValue('Field_C');
   }
}

Brad Bowman
Kilo Patron
Kilo Patron

Did this answer your question?

Hey Brad, no sir, if you noticed I tried that as a solution already. All three of the solutions I showed in my original post failed. It seems nothing identifies the oldValue as being empty, including the one you added(which is my third tried example).

My solution is tested and working given the understanding and conditions explained in my response.  If it is not working for you then you either have incorrect field name(s) or are doing something different than the test case that I outlined.