onChange client script to determine if a field was empty or not before the value in the field changed

patricklatella
Mega Sage

hi all,

I'm working on an onChange client script on the Problem table that I want to throw one field message if the field was empty before the change, and another message if the field was not empty when the change was made.   Seems simple enough, but I'm finding the using script like

if (oldValue != ''){

//then do something herre

}

doesn't work.   What is the use case for "oldValue" and "newValue"?

my script that is close, but not working is below.   Anyone see what's wrong?   Or have a better solution?   thanks!

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {

          return;

    }

//when a value is entered the first time

var prevValue = jQuery(control).attr('data-old-value') || '';    

// touching other form controls causes this to fire, so only respond when the newValue is valid

      if (newValue && newValue.length){

               

              if(prevValue.length == 0){

g_form.showFieldMsg('u_workaround', 'Please be sure to create Known Error Record after entering the Workaround.','info');

jQuery(control).attr('data-old-value', newValue);

return;

} else if (prevValue.length != 0){

g_form.showFieldMsg('u_workaround', 'Please be sure to update the Known Error Record after editing the Workaround.');

}

}

}

1 ACCEPTED SOLUTION

Patrick,



OldValue is unknown on load so use below code:



if (g_scratchpad.u_input == '' && newValue !='')


alert('1');


if (g_scratchpad.u_input != '' && newValue !='')


alert('2');



This will work for sure.


View solution in original post

15 REPLIES 15

Michael Fry1
Kilo Patron

oldvalue / newvalue pertain to the field you have selected in your onChange client script.



in this example, Priority is the field selected, so oldvalue / newvalue would be based on priority field



Screen Shot 2018-02-08 at 8.28.32 PM.png


patricklatella
Mega Sage

thanks for the reply Michael.



yes that's what I would think.   however when I script the logic like



if (oldValue == '')   to ask "if the old value was empty", it doesn't work.  



Am I using it correcty?


That should be fine but in your script you posted, I don't see where that line is called out.


patricklatella
Mega Sage

I would think something like this would work, but it doesn't.



if (oldValue ==   ''){   //looking to see if the field was empty


//do some logic here


} else {


do some other logic here


}