Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client-side UI Action to clear form fields without page reload (New Record)

akhilrajt
Giga Contributor

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:

 

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 don't want to cleared
            if (fieldName !== 'sys_id' && fieldName !== 'number')
    {
                g_form.setValue(fieldName, '');
               
            }
       
        g_form.addInfoMessage('Form fields have been cleared.');
    }
}
}
 

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!

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@akhilrajt 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

i tired with the above proposed solution, it did not work.

@akhilrajt 

it worked for me and cleared the fields, showed info message and stayed on form without reloading

I tested in Firefox and Chrome

clear form fields.gif

clear form fields chrome.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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 .