reload form on client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2016 12:33 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2016 12:39 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2016 12:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2016 12:45 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2016 12:50 PM
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;
}