Skip to main content
New Participant
January 18, 2018
Answered

Need help with if then statement

  • January 18, 2018
  • 1 reply
  • 1486 views

I have a calculated field "C". It is the result of subtracting two other fields, "A" and "B".

I need the value of "C" to populate a new field, "D" if the value of "C" is > 0 and populate "E" if the value of "C" is < 0.

I'm sure this is simple, but I would appreciate your help.

Thanks!

This topic has been closed for replies.
Correct answer try67

Thom's code is good, but not sufficient. It should also clear those fields when the answer needs to go to the over one. Use this version, instead:

event.value = Number(this.getField("A").value) - Number(this.getField("B").value);

if (event.value > 0) {

    this.getField("D").value = event.value;

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

} else if (event.value < 0) {

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

    this.getField("E").value = Math.abs(event.value);

} else if (event.value==0) {

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

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

}

Edit: Code fixed

1 reply

Thom Parker
Inspiring
January 18, 2018

You'll need to use a Custom JavaScript calculation for this:

event.value = this.getField("A").value - this.getField("B").value;

if(event.value > 0)

  this.getField("D").value = event.value;

else if(event.value < 0)

  this.getField("E").value = event.value;

This code does exactly what you requested. However, it does not cover the situationd you did not mention

  1. What happens when "C" == 0 ?

   2. What happens to D and E when the other field gets the value?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
New Participant
January 18, 2018

Thanks so much, Thom! Your answer is SO close!!

If the answer is 0, nothing needs to go in either field, since no money will go to anyone.

But your question about what happens to the other field once we know where the answer goes is spot on. Only one field needs to be filled in--"D" if the answer is positive or "E" if the answer is negative. How can I get it to give me only one answer? It seems that since I have to type in the "A" field info first, it automatically assumes the result will be positive, even if the final result is negative (because there are more debits than credits in the fields)--see below

(Lastly, is there a way to get rid of the minus sign ("-") for the Amount due? If not, no worries. You've been a big help!

try67
try67Correct answer
Braniac
January 18, 2018

Thom's code is good, but not sufficient. It should also clear those fields when the answer needs to go to the over one. Use this version, instead:

event.value = Number(this.getField("A").value) - Number(this.getField("B").value);

if (event.value > 0) {

    this.getField("D").value = event.value;

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

} else if (event.value < 0) {

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

    this.getField("E").value = Math.abs(event.value);

} else if (event.value==0) {

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

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

}

Edit: Code fixed