Skip to main content
Known Participant
August 8, 2022
Question

Conditional Java works BECAUSE of error. Help solve?

  • August 8, 2022
  • 2 replies
  • 1030 views

PDF Form has a field for Weekly Hours (totalField) and 7 fields for days of the week(Mon 1, Tue 1, Wed 1, etc.)

1. user can enter their total into totalField; or 

2. user can enter their hours for each day of the week. totalField becomes readonly and calculates.

 

My code is not perfect. It works, but it has an error (InvalidSetError: Set not possible, invalid or unknown. Field.value:46). The error arises if 1. user enters their total into totalField. The form functions just as it should. But I would like to learn how to do this without relying on the error. Error line labeled below:

 

var Sun = Number(this.getField("Sun 1").valueAsString);
var Mon = Number(this.getField("Mon 1").valueAsString);
var Tue = Number(this.getField("Tue 1").valueAsString);
var Wed = Number(this.getField("Wed 1").valueAsString);
var Thu = Number(this.getField("Thu 1").valueAsString);
var Fri = Number(this.getField("Fri 1").valueAsString);
var Sat = Number(this.getField("Sat 1").valueAsString);
var totalField = this.getField("Total Field");

var schedTotal = 0;
if (!isNaN(Sun)) schedTotal+=Sun;
if (!isNaN(Mon)) schedTotal+=Mon;
if (!isNaN(Tue)) schedTotal+=Tue;
if (!isNaN(Wed)) schedTotal+=Wed;
if (!isNaN(Thu)) schedTotal+=Thu;
if (!isNaN(Fri)) schedTotal+=Fri;
if (!isNaN(Sat)) schedTotal+=Sat;


// if schedule is empty, Total Field text and Day field boxes are light gray and all editable//
if(schedTotal == 0 && totalField == "Total") {
totalField.value = "Total";
totalField.textColor = color.ltGray;
totalField.readonly = false;
this.getField("Sun 1").strokeColor = color.ltGray;
}
//if validnumber entered into day fields, Total Field calculates and text is black//
else if((schedTotal > 0) && (schedTotal <= 37.5)) {
totalField.value = schedTotal;
totalField.textColor = color.ltGray;
totalField.readonly = true;
this.getField("Sun 1").strokeColor = color.ltGray;
 
} else if((schedTotal == 0) && (totalField !== "Total")) {
totalField.value = "Total";                                                              // Error on this line//
totalField.textColor = color.ltGray;
totalField.readonly = false;
this.getField("Sun 1").strokeColor = color.ltGray;

} else if(schedTotal==0) {
totalField.textColor = color.ltGray;
totalField.value = "Total";
totalField.readyonly = false;
this.getField("Sun 1").strokeColor = color.ltGray;
 
}
This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
August 8, 2022

The code does not work because of the error. It causes it, because you're trying to apply a calculated value to a field while the user is entering a value into it manually. Hence the InvalidSetError on the value property of the field.

In order to achieve this you need to carefully think about how this should work, and then build the code around that logic. For example, what should happen if the user enters a value manually into the field, but then goes and edits one of the day fields? Should it overwrite their value and apply the calculated one? If no, how should the tool decide when to apply the calculated value, and when not to? etc.

dankpoochAuthor
Known Participant
August 8, 2022

Thank you for the clarificaiton and I agree that my logic is not correct. I am struggling to express the desired function properly, despite many iterations. 

As you infer, edits in the day fields should override a manual entry in the totalField, render totalField readonly and apply the calculated value. Also, if the day fields entries are removed, the totalField should return to Total, readonly = false.

dankpoochAuthor
Known Participant
August 8, 2022

@try67 can I also save the stroke color somehow in this variable:

var Sun = Number(this.getField("Sun 1").valueAsString);

 

So that I can replace:

this.getField("Sun 1").strokeColor = color.ltGray;

 

with 

Sun.strokeColor = color.ltGray;

Bernd Alheit
Community Expert
Community Expert
August 8, 2022

What is the format of totalField?

dankpoochAuthor
Known Participant
August 8, 2022

I've set no format for totalField.

 

If relevant, totalField does run the following scripts

//On Focus//

event.target.strokeColor = ["RGB",0.863,0.863,0.863];

event.target.borderStyle = border.u;

if (event.target.value==event.target.defaultValue) {

    event.target.value = ""; event.target.textColor = color.black; }

 

//on blur//

if (event.target.value=="") {

    event.target.value = event.target.defaultValue;

    event.target.textColor = color.ltGray; }

 

if (event.target.value > "37.5") {

    event.target.value = event.target.defaultValue;

    event.target.textColor = color.ltGray; }

 

try67
Community Expert
Community Expert
August 8, 2022

Remove the quotes if you want the value to be considered a number, and not a string.