What to do if OnLoad Client Script is clearing the Field Value, when the form Loads?

Yesh
Tera Contributor

Hi Team, 

I am trying to insert a new record into the table via Workflow, It is getting inserted properly. But clearing one Field value, whenever i open that record. When I am trying to insert the new Record manually, then it is not clearing any field value when the form loads. The Client script which is clearing the Field value is below:

function onLoad() {

//for the new record form load all approved periods. For existing record, load with selected period

if (g_form.isNewRecord()) {
var approvedPeriod = new GlideAjax('Journal_ClientUtil');
approvedPeriod.addParam('sysparm_name', 'getPeriodName');
approvedPeriod.addParam('sysparm_cUserId', g_user.userID);
approvedPeriod.getXML(PeriodResponse);
} else {

var month = g_form.getValue('u_month');
var year = g_form.getValue('u_year');
g_form.clearOptions('u_month');
g_form.clearOptions('u_year');

if (month != '' && year != '') {
var arrayMonth = new Array("indexoccupier", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
g_form.addOption('u_month', month, arrayMonth[month]);// It is clearing this Month Field Value
g_form.addOption('u_year', year, year);
}
}

}

Is there any way to fix this issue?

Thanks,

Yesh

1 ACCEPTED SOLUTION

Hi,

So there will be 2 records

1) which is already created from workflow and you open it; it should give g_form.isNewRecord() -> false

2) which is created manually now and it's a new one; it should give g_form.isNewRecord() -> true

What's your case?

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so any existing record if you open your script is running?

ideally it should not since you have given isNewRecord()

did you check what comes in g_form.isNewRecord() in alert?

Regards
Ankur

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

Hi @Ankur Bawiskar ,

The scripting is running fine and it is not clearing any values of the fields when I open existing record, which is created manually. The scripting is clearing the field value of an existing record, which is created via Workflow.

When i am opening the new record, then g_form.isNewRecord() is giving the value true. So it is executing the if block statements. And for existing records, g_form.isNewRecord() is giving the value false, so it is executing the else block statements. 

Hope I have answered your queries.

Regards,

Yesh

Hi,

So there will be 2 records

1) which is already created from workflow and you open it; it should give g_form.isNewRecord() -> false

2) which is created manually now and it's a new one; it should give g_form.isNewRecord() -> true

What's your case?

Regards
Ankur

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

@Yesh

Any update on this?

Regards
Ankur

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

Hi @Ankur Bawiskar ,

It got resloved. I have created another client script to run for the existing record.

Thanks

Yesh