Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

onChange Catalog Client Script oldValue is empty

PabloL
Tera Contributor

Hi Community! Happy New Year!

I wrote an onChange catalog client script on a Variable named "Pickup Choice". This variable is type "Select Box" and has a choice list with choices like "Pickup" and "Ship to my Location".

 

The purpose of this script is to clear the mandatory status of the comments variable, which is made mandatory by a UI Action before the pickup choice is changed.

 

My issue that I found is that I tried to make the client script run only when oldValue == "Ship to my location" to signify the user selecting a different choice. However, I noticed that when I selected a new option, the oldValue was empty (but the newValue was correctly populated with the new choice).

 

PabloL_0-1673023277742.png

PabloL_0-1673024258257.png

 

 

Why could I not have access to oldValue? I'm trying to do this from the Platform UI with UI16 on a simple Catalog Item.

1 ACCEPTED SOLUTION

You might have to be sneaky with it. There is no direct way I'm afraid.

Create another variable that is not visible to the user ever and populate the value user selects each time in the field in question and set it in the hidden variable, read it from there when you need.

 

-Anurag

View solution in original post

5 REPLIES 5

Domi727
Tera Expert


As a workaround, I cache the last known value in sessionStorage and treat that as the “previous value” on subsequent onChange events:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      sessionStorage.clear()
      return;
   }
	var oldItemValue = sessionStorage.getItem('item');
	sessionStorage.setItem('item', newValue);

	if (oldItemValue) {
		// code here...
	} else {
		// code here...
	}
}