Copy link to clipboard
Copied
The following calculation script works to loop through 20 fields and if 5000 value is present, it will check Choice1 (yes) but when it is less than 5000, it won't check Choice2 (no).
var checkboxField = this.getField("CheckBox");
var checkField = false;
for (var i=1; i<=20; i++) {
var num = (i < 10 ? "0" + i : i);
var price = Number(this.getField("Price" + num).valueAsString);
if (price >= 5000) {checkField = true; break;}
}
checkboxField.checkThisBox(0, checkField);
Copy link to clipboard
Copied
The zero in your last line is only operating on the first widget of the check box series, so it will check or uncheck the first widget, depending on the value of checkField. Try setting the value of the field instead of checkThisBox like this:
var checkboxField = this.getField("CheckBox");
var checkField="Choice2";
for (var i=1; i<=20; i++) {
var num = (i < 10 ? "0" + i : i);
var price = Number(this.getField("Price" + num).valueAsString);
if (price >= 5000) {checkField = "Choice1"; break;}
}
checkboxField.value = checkField;
Copy link to clipboard
Copied
You can use this:
var cb = this.getField("CheckBox"),b=0,s=0;
for(var i=1; i<=20; i++){
var v = this.getField("Price" + (i<10 ? "0" + i:i)).valueAsString;
if(v && !isNaN(v)) +v >= 5000 ? b=1:s=1;}
cb.value = b ? "Choice1" : s ? "Choice2":"Off";
Copy link to clipboard
Copied
The zero in your last line is only operating on the first widget of the check box series, so it will check or uncheck the first widget, depending on the value of checkField. Try setting the value of the field instead of checkThisBox like this:
var checkboxField = this.getField("CheckBox");
var checkField="Choice2";
for (var i=1; i<=20; i++) {
var num = (i < 10 ? "0" + i : i);
var price = Number(this.getField("Price" + num).valueAsString);
if (price >= 5000) {checkField = "Choice1"; break;}
}
checkboxField.value = checkField;
Copy link to clipboard
Copied
So Choice2 is the false argument? So it loops and if any fields are 5000 or greater, it will trigger Choice1. If not, it breaks, skips below and checkboxField.value then becomes equal to checkField triggering Choice2?
Copy link to clipboard
Copied
Almost. The loop breaks as soon as price >= 5000 triggers Choice1. If it is never triggered, checkField remains as Choice2.
Copy link to clipboard
Copied
Ah okay. So how does {checkField = "Choice1"} work then?
Copy link to clipboard
Copied
While functional, this script treats empty fields as 0. That means "Choice2" will still be selected even when all fields are empty, which might not be the desired behavior.
If you want to avoid setting any checkbox value when fields are empty, you could add a check to skip empty fields or only apply the logic if at least one field has a valid number.
Copy link to clipboard
Copied
Yes it is the desired behavior. But since you mention it, how would you skip empty fields or apply logic only if at least one field has a valid number? Might be nice to know for future.
Copy link to clipboard
Copied
You can use this:
var cb = this.getField("CheckBox"),b=0,s=0;
for(var i=1; i<=20; i++){
var v = this.getField("Price" + (i<10 ? "0" + i:i)).valueAsString;
if(v && !isNaN(v)) +v >= 5000 ? b=1:s=1;}
cb.value = b ? "Choice1" : s ? "Choice2":"Off";
Copy link to clipboard
Copied
Thank you sir that works but how does that code work?
Copy link to clipboard
Copied
It sets the value of the variable checkField to "Choice1". The last line sets the value of the checkbox to the value of the checkField variable.
var checkboxField = this.getField("CheckBox");//define variable for CheckBox field object
var checkField="Choice2";//Set its default value to Choice2
for (var i=1; i<=20; i++) {//Loop through fields and obtain values (prices)
var num = (i < 10 ? "0" + i : i);
var price = Number(this.getField("Price" + num).valueAsString);
if (price >= 5000) {checkField = "Choice1"; break;}//if price is >= 5000, set checkField Choice2 and stop the loop
}
checkboxField.value = checkField;//set CheckBox value to checkField variable
Copy link to clipboard
Copied
Did you mean:
//if price is >= 5000, set checkField Choice1 (not Choice2) and stop the loop
}
In other words, if it isn't Choice1, it will stop the loop and revert back to the default value which is Choice2?
Copy link to clipboard
Copied
Yes
Copy link to clipboard
Copied
Thank you sir.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now