Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron
Part of the Tips 'N Tricks" series.

 

Just wanted to remind people that the "oldValue" parameter in Client Scripts is actually the value of the field when the form loaded and is NOT necessarily the value of the field/variable just before it was changed.  Let me show you an example.

 

Here, we will use one of the OOTB demo records on the Incident table.  It has a Short description of "Email server is down.":

 

find_real_file.png

 

Adding this Client Script will show the value of the Short description field in the browser's console window:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
		console.log("isLoading oldValue = " + oldValue);
		console.log("isLoading newValue = " + newValue);
        return;
    }

    console.log("oldValue = " + oldValue);
    console.log("newValue = " + newValue);
}

 

When we first open the record, the following is shown in the console:

 

find_real_file.png

 

If we make a change to the field, this shows up in the console:

 

find_real_file.png

 

And when we make another change:

find_real_file.png

 

Notice how the "oldValue" is always "Email server is down.".  I wish ServiceNow would give us a "previousValue" parameter that would contain the value just before the current change.  Then we could set a value back to the previous value if the current change did not meet some condition or validation we may be performing.

 

Luckily we don't have to wait for that missing parameter.  For fields that we may want to roll back to a previous value, we could use some code similar to this:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        g_scratchpad.previousShortDescription = newValue;
        console.log("isLoading oldValue = " + oldValue);
        console.log("isLoading newValue = " + newValue);
        return;
    }

    console.log("oldValue = " + oldValue);
    console.log("g_scratchpad = " + g_scratchpad.previousShortDescription);
    console.log("newValue = " + newValue);

    //make sure the word "cool" is in the Short description
    if (newValue.indexOf("cool") < 0 && newValue != g_scratchpad.previousShortDescription) {
        console.log("rolling back...");
        g_form.setValue("short_description", g_scratchpad.previousShortDescription)
    } else {
        //it's valid, let's keep track of the value for the next roll back
        g_scratchpad.previousShortDescription = newValue;
        console.log("scratchpad updated!");
    }
}

 

We set the value of the field onto the g_scratchpad object when we load the form and then whenever a "valid" change is made.  This then allows us to "rollback" to the last valid value in the field and not always all the way back to the beginning of time.

 

So now if we load the record and change the Short description to "first update", we get the following in the console:

 

find_real_file.png

 

...and the field is set back to "Email server is down.".  If we change it to "this is cool", we see:

 

find_real_file.png

 

...and our edit stays unchanged.  Then with our last test, change the field to "3rd update" and we get:

 

find_real_file.png

 

...with the field rolling back to "this is cool".

 

Et voila, we've now got a mechanism to keep track of the last valid values entered into a variable/field.  And obviously, the "console.log" statements can be removed in your production code  🙂