How to create Reset button in catalog item

Vaishnavi10
Tera Contributor

Hi Team,

I want to add a button in my catalog item as Clear Values and on clicking the button, it should clear the field values. 

Please help.

 

Thanks in advance.

11 REPLIES 11

Jaspal Singh
Mega Patron
Mega Patron

Hi Vaishnavi,

 

Follow link that has solution that fits your business case.

Its a simple widget which will be used in variable of type Macro

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vaishnavi,

you can use this code in the UI action; It should be client side UI action

Onclick: clearFields();

Script:

function clearFields(){

var allFields = g_form.getFieldNames();

        for(var fieldName in allFields){

                  g_form.clearValue(fieldName);

        }

}

Regards
Ankur

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

asifnoor
Kilo Patron

Hello Vaishnavi,

you can try something like this in your script. This way, you don't need to hardcode any fieldname.

var allFields = g_form.getFieldNames();
for(i=0;i<allFields.length;i++){
  g_form.clearValue(allFields[i]);
}

Mark the comment as a correct answer and also helpful if this helps to solve the problelm.

Willem
Giga Sage
Giga Sage

To also have it working in the Service Portal use this as UI Action:

Client: true

onclick: clearValues();

Script:

function clearValues() {
    var allFields = g_form.getFieldNames();
    for (i = 0; i < allFields.length; i++) {
        g_form.setValue(allFields[i], '');
    }
}