Restrict onChange script from executing on form load

A_112
Kilo Contributor

Hi,

I have a catalog client script of type onChange. This script makes check in if condition and then throws a field message if check evaluates to true. Issue I am facing is that this field message is occurring when page loads.

Scenario:

I have a date field that pre-populates when page loads. This date is set to some date in future. I am maintaining this same date in a hidden date/time field as well. When user changes this date to even one day beyond the pre-populated date, user gets a field message info.

Basically when user changes the date, an onChange script on this date/time field executes and I do a comparison between this changed date and the original date which is also stored in hidden date/time field. If changed date is more than date in hidden field then the user sees the field message info. This scenario works fine.

But this field message info is also appearing when the form loads and sets the pre-populated date in the visible date/time field and hidden date/time field from back end table.

 

I referred to some solutions shared on community but no luck. I do not want this onChnage script to run when form loads.

Below are the articles I referred:

https://community.servicenow.com/community?id=community_question&sys_id=82964f25db1cdbc01dcaf3231f961901

https://community.servicenow.com/community?id=community_question&sys_id=67c88b61db5cdbc01dcaf3231f9619da

https://community.servicenow.com/community?id=community_question&sys_id=6e501729dbdcdbc01dcaf3231f961936

 

Thanks

1 ACCEPTED SOLUTION

Hmm, ok i see the problem; the oldValue is always going to be the value of the field when the form loaded so that condition will always match.

You could try this, i have no idea if it will work but worth a try!

if(oldValue == ''){
oldValue = newValue;
return;
}

If that doesn't work (and i suspect it won't) you can add a hidden true/false field to the form, default it to false and in the onLoad script that calls the nowDateTime function, after the line that populates the date/time field add another line to change the true/false field to true. The in your onChange you can say:

if(g_form.getValue('u_true_false_field') == false){
return;
}

 

View solution in original post

6 REPLIES 6

Hmm, ok i see the problem; the oldValue is always going to be the value of the field when the form loaded so that condition will always match.

You could try this, i have no idea if it will work but worth a try!

if(oldValue == ''){
oldValue = newValue;
return;
}

If that doesn't work (and i suspect it won't) you can add a hidden true/false field to the form, default it to false and in the onLoad script that calls the nowDateTime function, after the line that populates the date/time field add another line to change the true/false field to true. The in your onChange you can say:

if(g_form.getValue('u_true_false_field') == false){
return;
}

 

A_112
Kilo Contributor

Hi,

I am able to get this working with 2nd approach i.e. setting 'u_true_false_field' value in onLoad script and checking in onChange script. Thanks for quick help.