How to get old value in On Submit Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello All,
I got a requirement where we have list of Risk fields on Change Form based on those questions Risk has to be calculated and it's getting calculated.
Now the problem is I need to show alert message to the user when user updates the related Risk fields and saves the form, I have written the below On Submit client script and it's showing the message but I need to show the message only when user updates the Risk Assessment values from one value to other but not on when user updates from Empty to Something(i.e., on Initial update) so please guide me on checking if the previous is empty or not.
On Submit Client Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
The easy way is to do this onChange instead of onSubmit, as the oldValue object is then made available. Another approach is to create a Display Business Rule on the table, writing the value when the record loads to the scratchpad. The script for this is simply
(function executeRule(current, previous /*null when async*/ ) {
g_scratchpad.shortdescription = current.request_item.short_description;
})(current, previous);
or you can get more advanced if needed. In the Client Script then, you can use something like
var oldvalue = g_scratchpad.shortdescription;
to access this field value as of when the form loaded.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Oldvalue in a client script only reflects the value from when the form loaded and does not give you the previous value after the user has already changed the field once or more.....so in an onsubmit script you cannot directly check a field previous (intermediate) value to compare against the latest value.......and the typical workaround used is to capture the initial loaded value early (for example by storing it in a hidden field or via g_scratchpad in an onload script).......and then in your onsubmit compare the current g_form.getValue(...) to that stored original.......
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Technical Consultant - ServiceNow Class of Legends 2025
