onChange script executing immediately after onLoad script runs on table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2019 01:21 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2019 01:28 AM
So you have a field that you're changing onLoad and an onChange script that runs on this field but you only want the onChange script to trigger when a user changes the field value, no when the onLoad script changes the field value?!
Ok, so maybe try configuring a display business rule to print the value of the field to the scratchpad when the form loads, this should happen before your onLoad script runs so in your onChange script you can use that value to compare against the oldValue and say:
if(oldValue == g_scratchpad.field_value){
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2019 04:51 AM
I tried to set the auto populated date in a localStorage variable and did this check:
if(oldValue != localStorage.getItem("expiration_date_l")){
g_form.clearMessages();
return;
}
This helped with infoMessage not showing up immediately after form loads i.e. rest of the onChange script is not executing with this change. But this check is done everytime this onChange script executes when I manually change the date and the script simply returns without executing the onChange script. This happens even in case of g_scratchpad variable. Since g_scratchpad is used in the current page scope, it is checked in onChange script everytime I manually change the date.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2019 05:39 AM
Hi Tadhav, did you test with my code that i gave you earlier?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2019 06:07 AM
Ah, i see the problem, oldValue is always going to be the original value of the field before the onLoad script made it's changes so oldValue will always equal the scratchpad value.
The only thing i can think of is to add a field with a default value of 0 and increment that value every time the onChange script fires. That way you can stop it from processing when the value is 0 ie, the first time it fires as a result of the onLoad script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var number = parseInt(g_form.getValue('u_field_name'));
if(number == 0){
number = number + 1;
return;
}
else{
//add your code in here
number = number + 1;
return;
}
}