Onchange Client script previous value

BenG
Tera Guru

Hello,

Is there a way to catch the previous value of a Onchange client script ?

Not the oldValue (first value after a page load) by the previous value after the newValue ?

I did that with a custom field "previous value" who copy the newValue every time to compare with the value of the field "previous value".

It works well by I would like to know if there is a better way to do that stuff. (I don't wont to create fields everytime I need a previous value ;-))

Thanks a lot for your help.

Regards, 

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Are you saying the database value actually present for the field when the form loads?

If yes then below approach you can try

You can try to use display BR which would run once when form loads and stores the actual database value of that field in scratchpad variable

This scratchpad variable is accessible in client script

Display BR:

g_scratchpad.myFieldValue = current.<fieldName>;

Client Script:

alert(g_scratchpad.myFieldValue);

Regards
Ankur

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

Hi,

My problem is different. When I'm in incident form some field value can change several times.

For exemple priority can change several times after form load (users can do a mistake and change priority again without saving the form). I would like to catch the last previous value to compare this value to newValue in a Onload client script.

When I use Onchange parameter oldValue, I have the first value after a form load but I would like the (last) previous value who has changed.

Thank you or your help,

Hi,

Can you explain your business use-case/scenario with example

Regards
Ankur

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

BenG
Tera Guru

Exemple:

OnChange script on select box "testing" with values (test1, test2, test3)


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

if (oldValue != newValue) {

alert("OK");

}


when we open a existing form incident with value test1 in the field "testing" and I change the selectbox field by test2

odlValue = test1
newValue = test2

And I change the selectbox field again by test1

odlValue = test1
newValue = test1

at this step I would like to catch the test2 value so the previous state of the field