If then question

Explorer ,
Jan 13, 2018 Jan 13, 2018

Copy link to clipboard

Copied

If anyone can help with a scenario that I'm struggling with I would be eternally grateful.  I am learning how to write in Javascript but am very new to this.  I'll do my best to describe what is happening and how I'm trying to write the script.  Below is the breakdown:

Line "J10" is a sum of 5 lines.

Line "J13" is the sum of line "J10" and "J12" ONLY if Line "J10" is greater than 0.  If Line "J10" is a negative number then Line "J13" should return 0.  I've written the script so many different ways I think I've thoroughly confused myself.  At one point, I thought I solved it.  When I enter the number and it gave the result I was looking for on the positive side and then changed the number to a negative but the result from the previous script didn't change but stayed the same.  I want to be able to write the Javascript so if I test it with a positive result it gives the result I'm looking for and if I change the test number to a negative number that it switches to a 0. It's almost as if the script is unstable.  Any input on how to write this would be so greatly appreciated.  Thank you in advance.

Here is the latest that doesn't work.

var theField = this.getField("V1");

var theValue = theField.value;

var theField2 = this.getField("V2");

var theValue2 = theField.value2;

if (theValue > 0) {

    this.getField("J13").value= theValue2;

}

else {

     this.getField("J13").value= 0";  

}

TOPICS
Acrobat SDK and JavaScript, Windows

Views

197

Likes

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

Adobe Community Professional , Jan 14, 2018 Jan 14, 2018
Following line makes no sense: event.value > (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5 + +theValue6 + +theValue7);  You can't use 2 elseYou must use something like this:if (...) {} else if ( ... ) {} else {}

Likes

Translate

Translate
Adobe Community Professional ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

A field has no property with the name 'value2'.

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Thank you for the response.  I understood that I'm getting that error.  Any suggestions what I can write to eliminate the error and get the result I'm looking for?

Likes

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
Adobe Community Professional ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Change that line to:

var theValue2 = theField2.value;

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Thank you, the resolved that problem.  Interesting, the script works, however, if I change thevalue  the result remains the same and doesn't update with the new value.  Is there a script or a an alteration I can put in my script that will update when I'm changing the value?  Thank you again.

var theField = this.getField("V1");

var theValue = theField.value;

var theField2 = this.getField("V2");

var theValue2 = theField2.value;

if (+theValue > .001) {

    this.getField("J13").value= +theValue2;

}

else {

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

}

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

One other thing sometimes when I put a value in nothing is computing for my final result.  If I tweak it suddenly the result appears.  Not sure why it doesn't populate result and then it appears all of a sudden.

Likes

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
Adobe Community Professional ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Check the field calculation order in the form.

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Great advice everyone!  Everything seems to be in working order.

Thank you!

Likes

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
Adobe Community Professional ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Also, if this is a calculation script for "J13" replace these lines:

this.getField("J13").value = ...

With:

event.value = ...

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

More great advice.  Of course, now I ran into another error.  If there is any advice on this I think this will function exactly the way i'd like.  Once I implemented the changes everything works except I added one more thing.  So, I have an "if" statement that provides me an event result, and I have an else statement that provides me a result, I then added one more "else" statement below.  I tried make this an event.value result but I still get the same syntax error JavaScript error: SyntaxError: expected expression, got keyword 'else' on line 26.  All I'm trying to do is get a result of "o" if the "if" or "else" or not true.

}

else {

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

}

Likes

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
Adobe Community Professional ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Post your full code.

On 14 January 2018 at 16:01, stepheng54012748 <forums_noreply@adobe.com>

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Here is what I have so far:

var theField = this.getField("J5");

var theValue = theField.value;

var theField2 = this.getField("J6");

var theValue2 = theField2.value;

var theField3 = this.getField("J7");

var theValue3 = theField3.value;

var theField4 = this.getField("J8");

var theValue4 = theField4.value;

var theField5 = this.getField("J9");

var theValue5 = theField5.value;

var theField6 = this.getField("J11");

var theValue6 = theField6.value;

var theField7 = this.getField("J12");

var theValue7 = theField7.value;

if (+theValue <= 0) {

    event.value = (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5);

}

else {

     event.value > (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5 + +theValue6 + +theValue7);  

}

else {

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

}

Likes

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
Adobe Community Professional ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

Following line makes no sense:

event.value > (+theValue + +theValue2 + +theValue3 + +theValue4 + +theValue5 + +theValue6 + +theValue7); 

You can't use 2 else

You must use something like this:

if (...) {

} else if ( ... ) {

} else {

}

Likes

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
Explorer ,
Jan 14, 2018 Jan 14, 2018

Copy link to clipboard

Copied

LATEST

Working perfectly now!! Thank you

Likes

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