Want to clear the form field after click on insert and stay ui action

adnanjaved
Kilo Contributor

I have created a insert and stay ui action on which i have to insert the current record and clear the certain field. I am able to insert the record. but the field does not gets clear.Here goes my script.

function doInsertAndStay() {

var saveMe = current;

if (typeof current.number != 'undefined' && current.number)

current.number = ""; // generate a new number

else if (typeof current.u_number != 'undefined' && current.u_number)

current.u_number = ""; // generate a new number

current.insert();

action.setRedirectURL(saveMe);

g_form.setValue('number', ' ');

g_form.setValue('u_part_number', ' ');

}community

2 REPLIES 2

Geoffrey2
ServiceNow Employee
ServiceNow Employee

You are trying to perform both server-side (current) and client-side (g_form) actions within the same function. It's not going to work like that.


What you need to do is create the record, but somehow know to clear those fields when the form is reloaded, without it affecting the same form the second time you load it.


Clearing the fields needs to happen on the client-side, so really, you need a way to communicate between form submission and form load.



There are at least 3 ways to do this that I can think of right now:


  1. User Preference - Within your UI Action, create a User Preference record. Set the logged in User as the User. Set the name to something meaningful, like "<table_name>.clear_numbers".
    Create a Display Business Rule on that table that looks for that User Preference record. If the User Preference Record exists, clear the fields you want to clear and delete the User Preference.
  2. localStorage - Update your UI Action to a Client-side UI Action. When the UI action is clicked, set a value in the localStorage - something like "localStorage.setItem('clear_numbers', true);", then save the record.
    Create an onLoad Client Script that checks for the localStorage item. If it exists, clear the fields you want, and clear the localStorage
  3. URL parameter - In your existing UI Action, update your redirect URL to include an additional parameter: "clear_numbers=true". When your form reloads, you want that new parameter to appear in the URL.
    Create an onLoad Client Script that checks for the URL parameter. If it exists, clear the fields you want.

Thanks for to the point answer. Your second option helped me in solving my problem.