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

@akhilrajt 

but why are you using setValue() and not clearValue() since you want the value to be cleared

it worked for me with setValue() and no browser message

I checked in Firefox

Seems some configuration in your browser is doing this

But you should use clearValue() as setValue() might trigger some onChange client script

💡 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

thank you for you support. i will have to check if there is any configuration that is messing with the browser and update accordingly