Skip to main content
June 15, 2016
Answered

Text field subtraction

  • June 15, 2016
  • 1 reply
  • 640 views

I have 3 text fields in a pdf and I named them each Adjustments_1 , Adjustments_2 and Adjustments_3. I want Adjustments_3 to equal  Adjustments_1 - Adjustments_2 and if that difference is less than 0, I want Adjustments_3 to equal "0" and have that difference displayed on the text fields. I tried doing an if else statement under the custom calculation script in the Adjustments_3 properties which looks like this:

if (Adjustments_2 < Adjustments_1) {

Adjustments_3=Adjustments_1-Adjustments_2;

}

else {

Adjustments_3 = "0";

}

My difference is always 0.00 for any value I try, so I'm just wondering where I went wrong.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

You cannot access the value of a field by just using the field name. The correct way to get the value of a field is this:

this.getField("Adjustment_1").value

So, to convert your calculation to this mechanism, by also introducing JavaScript variables, the script would look like this:

var Adjustment_1 = this.getField("Adjustment_1").value;

var Adjustment_2 = this.getField("Adjustment_2").value;

var Adjustment_3;

if (Adjustments_2 < Adjustments_1) {

    Adjustments_3=Adjustments_1-Adjustments_2;

}

else {

    Adjustments_3 = "0";

}

this.getField("Adjustment_3").value = Adjustment_3;

As you can see, I've left your script intact, and created variables with the names that you've used, but either assigned these variables based on the field value, or assigned the field value based on the variable Adjustment_3.

1 reply

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
June 15, 2016

You cannot access the value of a field by just using the field name. The correct way to get the value of a field is this:

this.getField("Adjustment_1").value

So, to convert your calculation to this mechanism, by also introducing JavaScript variables, the script would look like this:

var Adjustment_1 = this.getField("Adjustment_1").value;

var Adjustment_2 = this.getField("Adjustment_2").value;

var Adjustment_3;

if (Adjustments_2 < Adjustments_1) {

    Adjustments_3=Adjustments_1-Adjustments_2;

}

else {

    Adjustments_3 = "0";

}

this.getField("Adjustment_3").value = Adjustment_3;

As you can see, I've left your script intact, and created variables with the names that you've used, but either assigned these variables based on the field value, or assigned the field value based on the variable Adjustment_3.

June 15, 2016

Thank you so much, it worked perfectly!