Client script oldValue always empty

Brian Lancaster
Tera Sage

I am writing an on Change script for the change module.   When I try and if (newValue == 'This' && oldValue == 'that') it is not working.   So I did a alert(newValue) along with an alert(oldValue) and oldValue is away returning as empty.   What might be causing oldValue not to return anything?   Just note that newValue is always returning what I expect it to.

13 REPLIES 13

Anurag Tripathi
Mega Patron
Mega Patron

Hi Brian,



Sorry it was a long thread and didn't read through all. Apologies if i am stating what is already established here.



Old Value comes from the Database when the form is loaded, unless the firm is saved again the old value will not change. So on a new record, unless you save it the old value will always return null.


-Anurag

ccarver
Tera Contributor

If you come across this post here is the answer as of March 2024.

You need to leverage sessionStorage functions on the client side. See the universal documention covering sessionStorage.

What you need to do is have a client script that onChange to store the field value using

sessionStorage.setItem("key", newValue);

Then when you want to see the "old" value - which was set prior using the above

var data = sessionStorage.getItem("key");

It basically boils down to you are keeping a secondary copy of the value in the session to extract later.

I just tried testing this out. It is not consistent. It only seems to work the first time you change the value. I did have to modify the first line you gave otherwise it never worked.

sessionStorage.setItem("key", oldValue);

 I don't even 100% remember why I was trying to even to this. I have not tried to use oldValue in a client script since this initial post.

AkshayKangankar
Giga Guru
Giga Guru

Hi @Brian Lancaster 

What I understood, your requirement was get old and new value in every change. Can you try below code 

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

  // Initialize the previous value variable if undefined
  if (g_form._previousValue === undefined) {
    g_form._previousValue = '';
  }

  alert('Old Value: ' + g_form._previousValue + '\nNew Value: ' + newValue);

  // Update the stored old value for next change
  g_form._previousValue = newValue;
}

 

 

Regards,
Akshay Kangankar