reload form on client script

sangeethasingh1
ServiceNow Employee
ServiceNow Employee

Hi,

I wrote onchange client script which checks if the value in a filed   is valid or not. If it is invalid i need to reload the form so that it get the previous value on that field.

How should i do that in client script. Please let me know.

Thanks

9 REPLIES 9

venkatiyer1
Giga Guru

Hi,



On change script has the new value and old value. Assign the old value back using control.value instead of g_form and I dont think you need to reload the form. Alternatively, you can just return false if the newValue is not valid so that it doesnt set the change at all.


Chuck Tomasi
Tera Patron

Hi Sangeetha,



On Change client scripts include two parameters that may be of interest to you, oldValue and newValue. If you want to set the value back, just use oldValue.



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


    if (isLoading || newValue === '') {


          return;


    }




    //Type appropriate comment here, and begin script below


   


}


Client Scripts - ServiceNow Wiki


Hi Chuck,



The problem is that if i am setting the old value then again the onchange event is fired and it going into loop. Hence i wanted to reload the form



Thanks


Hi Sangeetha,



Setting the old value from within the same client script should not re-trigger the same client script. That would be become an endless loop.



Somewhere inside the function you have a check like:



if (!myConditionIsTrue) {


g_form.setValue('my_field', oldValue);


return;


}