onChange script executing immediately after onLoad script runs on table

A_112
Kilo Contributor

HI.

 

I have written 2 scripts on a custom table. One script is of type onLoad and another of type onChange.

onLoad script sets a calculated date in a date/time field. When I click on 'New' to add new record on this table, form view opens, onLoad script executes and sets a calculated date in date/time field. Problem is as this value is set in date/time field, onChange script also executes.

onChange script should only execute when I manually change date in date/time field. This onChange script throws an info message if date set by onLoad script is changed to some other date. But info message is thrown immediately after form loads and date is set.

I tried below options:

 

1. In onChange script:

if(loading){
 return;
}

2. Compare oldValue & newValue:

In onChnage script, I expect oldValue to be the value set by onLoad script and newValue to be changed date value. So only when oldValue != newValue, the rest of the onChange script will execute.

if(oldValue != newValue){
 g_form.addInfoMessage('Date change not allowed.');
}

Issue is oldValue is blank every time I change the date i.e. oldValue does not maintain the old date value.

3. Created a hidden true/false field 'u_true_false_field' on the table and set its value to true immediately when onLoad script executes and perform below check in onChange script:

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

Kindly suggest a way to prevent onChange script from excuting.

 

Thanks

8 REPLIES 8

Dubz
Mega Sage

Actually before you try that last suggestion, i see you're using 

if(loading){
return;
}

The correct syntax is:

if(isLoading){
return;
}

Please confirm you're using the correct syntax to prevent the onChange running while the form is loading.

Omkar Mone
Mega Sage

Hi 

 

Try adding this on the first line of onChange - 

if (isLoading || newValue === '')
return;

Sajilal
Mega Sage

Ideally not recommended to set the field value on OnLoad, but if its a genuine scenario try having a Display business rule to store a scratchpad variable value of the date field, then you can use this field in the OnChange to check if it was the value from backend or onload you got a new value. this is the only way

Thanks,

SAjilal

Mark Correct if this solves your issue and also mark 👍 Helpful if it helps resolve your problem

asifnoor
Kilo Patron

Hi,

I can understand your problem as we have also faced this in our project. The best way to handle this is

if (isLoading || newValue === '' || (newValue.toString().length>0 && oldValue.toString().length==0)) {
return;
}

Mark the comment as a correct answer and helpful if this helps.