Copy link to clipboard
Copied
Ok, I've been searching for 3 days on this answer, but everytime I change something I break my form again.
I have 4 text boxes:
Two with Currency variables.
In the 3rd box, they need to subtract in.
In the 4th box, it needs to display Over/Short/Balanced
I've got the form working with just that information. My problem is that if the first two boxes are blank, the other two need to be blank. Or in otherwords, if a calculation hasn't taken place, Box 3 & 4 need to remain blank till Box 1 & 2 are populated.
In Box 3 for the Calculation Script, I've got this:
event.value = ( this.getField("Actual_Cash_in_TCR").value - this.getField("TCR_Reported_Totals").value );
In Box 4 for the Calculation Script, I've got this:
var n = this.getField("Amount").value;
if( n > 0) event.value = "Over";
else if ( n < 0) event.value = "Short";
else event.value = "Balanced";
Where am I messing up, what needs to be added and where?
Adobe Acrobat Pro DC 2015.006.30355 for Windows.
Copy link to clipboard
Copied
You can use an if/else construct to find out if the source fields are filled in. Something like this should work:
var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;
var val2 = this.getField("TCR_Reported_Totals").valueAsString;
if (val1 == "" || val2 == "") {
event.value = "";
}
else {
event.value = ( Number(val1) - Number(val2) );
}
Copy link to clipboard
Copied
I get SyntaxError: syntax error 3 for the "else" argument
Copy link to clipboard
Copied
The code from above works for me. Can you please post the exact code you are using.
Copy link to clipboard
Copied
Ok, I just copied your code exactly, now there's no error, but theres nothing populating in Box 3 (in the picture, it's "Amount Difference", technically Box 4)
var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;
var val1 = this.getField("TCR_Reported_Totals").valueAsString;
if (val1 == "" || val2 == "") {
event.value = "";
}
else {
event.value = ( Number(val1) - Number(val2) );
}
Copy link to clipboard
Copied
If you are using my script for the forth field, then it should work. Are you getting any errors on the console?
Copy link to clipboard
Copied
Nope, it allows me to enter it and save it.
Just doesn't populate the Amount Difference Field, Like something is missing, or it's taking the Blank command to heart.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You defined two variables called "val1", instead of the second one being "val2"...
Copy link to clipboard
Copied
Wow, ok! It's been a string of Mondays. Thank you!
Ok, now how about the other?
Over/Short (box 3)... I've got it to be blank to start, but it's "reading" it incorrectly or I'm inputting incorrectly
if I input 15 & 15, the OVER/SHORT should read "Balanced". It's showing blank.
for 16 & 15, the OVER/SHORT should read "Short" but it's still blank,
for 14 & 15, the OVER/SHORT should read "Over" but this time it displays "Short"
This is what I have for Box 3:
var val3 = this.getField("Amount").valueAsString;
if (val3 == "" ) {
event.value = "";
}
else if ( val3 > 0 ) event.value = "Over";
else if ( val3 < 0 ) event.value = "Short";
else if ( val3 = 0 ) event.value = "Balanced";
Copy link to clipboard
Copied
A value can't be a string and a number at the same time... Also, you have a syntax error in the last if-condition. Use this instead:
if (val3 == "" ) event.value = "";
else if ( Number(val3) > 0 ) event.value = "Over";
else if ( Number(val3) < 0 ) event.value = "Short";
else if ( Number(val3) == 0 ) event.value = "Balanced";
Copy link to clipboard
Copied
Unsuccessful...
doesn't val3 need to be defined?
Copy link to clipboard
Copied
You still need this line from your script:
var val3 = this.getField("Amount").valueAsString;
Copy link to clipboard
Copied
var val3 = this.getField("Amount").valueAsString;
if (val3 == "" ) event.value = "";
else if ( Number(val3) > 0 ) event.value = "Over";
else if ( Number(val3) < 0 ) event.value = "Short";
else if ( Number(val3) == 0 ) event.value = "Balanced";
Still getting some random displays.... 15/15 shows blank "OVER/SHORT", 15/14 is showing Balanced, 15/16 is showing Short, 15/17 is showing Over.
Copy link to clipboard
Copied
Is the value of your field literally "15/15"? That's not a number, although it can be converted to one...
Copy link to clipboard
Copied
Based on your message, I assume you mean something like this... It didn't produce errors, but still not calculating the correct output.
Just noticed something odd as well.
var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;
var val2 = this.getField("TCR_Reported_Totals").valueAsString;
var val3 = this.getField("Amount").valueAsString;
if (val1 == "" || val2 == "") {
event.value = "";
}
else if ( val3 > 0 ) {
event.value = "Over" ;
}
else if ( val3 < 0 ) {
event.value = "Short";
}
else {
event.value = "Balanced";
}
Copy link to clipboard
Copied
Change:
var val3 = this.getField("Amount").valueAsString;
To:
var val3 = Number(this.getField("Amount").valueAsString);
Copy link to clipboard
Copied
Everything is showing as "Balanced" now regardless of number
var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;
var val2 = this.getField("TCR_Reported_Totals").valueAsString;
var val3 = Number(this.getField("Amount").valueAsString);
if (val1 == "" || val2 == "") {
event.value = "";
}
else if ( val3 > 0 ) {
event.value = "Over" ;
}
else if ( val3 < 0 ) {
event.value = "Short";
}
else {
event.value = "Balanced";
}
Copy link to clipboard
Copied
To see what goes into your calculation, add the following just before the if statement:
console.println("val3 = " + val3);
Then check the console to see what val3 is being set to.
Copy link to clipboard
Copied
I can't help you further without seeing the actual file.
On Wed, Sep 20, 2017 at 5:16 PM, Throwzone2001 <forums_noreply@adobe.com>
Copy link to clipboard
Copied
Maybe this will help?
Copy link to clipboard
Copied
- You're not using the code we gave you. You still have the same error I mentioned in my first reply!
This is incorrect:
else if ( val3 = 0 ){
Either you follow our advice or don't, but don't say you do and then don't and say it's not working... That's very frustrating.
- Also, your field calculation order is incorrect. "Over-Short" needs to be calculated after "Amount".
Copy link to clipboard
Copied
This is what I have in now (unless I missed something)...
What do you mean about Field Calculation? I'm not trying to not follow advice, just getting it in different formats.
try67 looks like what I was starting with, and Karl's looks like SQL, getting confused. (why I didn't go into programming).
var val1 = this.getField("Actual_Cash_in_TCR").valueAsString;
var val2 = this.getField("TCR_Reported_Totals").valueAsString;
var val3 = Number(this.getField("Amount").valueAsString);
console.println("val3 = " + val3);
if (val1 == "" || val2 == "") {
event.value = "";
}
else if ( val3 > 0 ) {
event.value = "Over" ;
}
else if ( val3 < 0 ) {
event.value = "Short";
}
else if ( Number(val3) == 0 ) {
event.value = "Balanced";
}
Copy link to clipboard
Copied
Fields are calculated in a certain order. When you calculate a field that depends on another field value before that second field gets calculated, then the results are of course wrong - you are basing your calculation on old values.
Acrobat allows you to set the field calculation order. This is also done when you are in the form editor, but it's selected via a menu:
Copy link to clipboard
Copied
BTW: You don't need to test for zero: That's already implied - when a value is not greater than zero and not less than zero, it must be zero, so you can just use the else branch as you did before:
else {
event.value = "Balanced";
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now