Client-side UI Action to clear form fields without page reload (New Record)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi Community,
I am looking for support regarding a custom UI Action I created to reset a form.
Requirement: I have a "Reset Form" UI Action button that appears on the form before insertion (New Record). My goal is to clear all field inputs so the user can start over, but I want to stay on the current record context. I do not want to reload the page, as reloading seems to be initializing a new record/Sys ID rather than just resetting the current form.
The Issue: With my current code, the fields are cleared, but the browser triggers the "Reload site? Changes you made may not be saved" error. This suggests the script is trying to force a page refresh, which is not the desired behavior.
Configuration:
Client: True
Show Insert: True
The Code: Please see the script I am currently using below:
My Question: How can I modify my script to simply clear the field values on the client side without triggering a page reload alert?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
use g_form.clearValue() instead of setValue()
function reset_form_fields() {
var confirmReset = confirm("Are you sure you want to clear all fields on this form?");
if (confirmReset) {
var fields = g_form.getEditableFields();
for (var i = 0; i < fields.length; i++) {
var fieldName = fields[i];
// Exclude essential/system fields
if (fieldName !== 'sys_id' && fieldName !== 'number') {
g_form.clearValue(fieldName);
}
}
g_form.addInfoMessage('Form fields have been cleared.');
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
it worked for me and cleared the fields, showed info message and stayed on form without reloading
I tested in Firefox and Chrome
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
51m ago
i use chrome & brave and tested from both the browsers and it did not work.
can you please check using g_form.setValue() if there is any dialog message in the browser .
