• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help with if then statement

New Here ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

829

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 18, 2018 Jan 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").val

...

Votes

Translate

Translate
Community Expert ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

Thanks. I plugged the new info in and got an error message:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

My bad... Add an opening curly bracket in line 2:

if (event.value > 0) {

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

I must be doing something wrong. I added the opening bracket, but now get this error message:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

You added it in the wrong place. Copy the code from my previous reply. I fixed it there...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 18, 2018 Jan 18, 2018

Copy link to clipboard

Copied

LATEST

I figured I'd messed it up!

Thanks so much! It works perfectly!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines