Copy link to clipboard
Copied
I am trying to create a pdf javaScript formula that calculates if a field is greater than 2 multiple that field's value by 25 but not to exceed $250
Copy link to clipboard
Copied
Use the following custom calculation script:
var fld=this.getField("theField").value;
if(fld>2)
{event.value=fld*25}
else
{event.value=""}
if(event.value>250)
{event.value=250}
Copy link to clipboard
Copied
Use the following custom calculation script:
var fld=this.getField("theField").value;
if(fld>2)
{event.value=fld*25}
else
{event.value=""}
if(event.value>250)
{event.value=250}
Copy link to clipboard
Copied
Consider adding an 'else' condition to reset the field to blank or 0 if the value initially exceeds 2 but subsequently drops below 2.
Copy link to clipboard
Copied
Good point. Answer edited.
Copy link to clipboard
Copied
The order you added them is not correct, though.
It should be if - else if - else.
Copy link to clipboard
Copied
Yes you can do it that way and I usually would, but I made an adjustment to what I already had. My script works. The first if/else statement calculates the value. The next if statement takes that value and adjusts it down to 250 if it is > 250.
Copy link to clipboard
Copied
Thanks, that works perfectly.
Now, I trying to do something similar.
If the value of that field is greater than 2 there's a 5% tax added but if it's greater than 3 there's a 10% tax added.
Copy link to clipboard
Copied
Try this:
var fld = this.getField("theField").value;
var result = 0;
var maxLimit = 250;
if (!isNaN(fld) && fld > 2) {
result = fld * 25;
if (fld > 2 && fld <= 3) {
result = result * 1.05;}
else if (fld > 3) {
result = result * 1.10;}}
event.value = Math.min(result, maxLimit);
Copy link to clipboard
Copied
I guess I misspoken. The formula needs to indicate if the field is greater than 2, the value should be 5% and if the field is greater than 3 the value should be 10%
Copy link to clipboard
Copied
% of what? The result? Use Nesa's script but change 1.05 to .05 and change 1.10 to .1
Copy link to clipboard
Copied
Just ment to determine if the tax value is 5% or 10%. I'm using another field to calculate the value based on that field..
in other words I used the 1st java script to determine "this field " is greater han 2 multiple by 25.
I need new field that calculates if "the field" is greater than 2 the value = .5 and if "the field " is greater than 3 the values = .10.
I'm sorry I made it confusing
Copy link to clipboard
Copied
Can anyone help me with the following?
Complete newbie here, but tried adapting the above formula to perform the following:
var fld=this.getField("theField").value;
if(fld>110)
{event.value=fld*.5}
else
{event.value=""}
However I am getting a "Sytax Error: unterminated string literal 1: at line 2" error.
Any suggestions?
Copy link to clipboard
Copied
Are you sure this is the actual code you used?
Did you maybe use non-standard quotes in it?
Copy link to clipboard
Copied
Yes, only difference is that is substituted the field name for "theField" with the really long field name I was actually using.
Now I have an "enter" after each line. Does that matter (I do not know much about Java, just learning)
Copy link to clipboard
Copied
Post the actual script.
Copy link to clipboard
Copied
var fld=this.getField("PricePaidasapercentofParValueofGuaranteedInterest”).value;
if(fld>110)
{event.value=fld*.5}
else
{event.value=""}
Copy link to clipboard
Copied
Var and fld at the beginning are not on seperate lines, but looks like word wrap in this forum pushes it to next line because of the size of the next segment without spaces or natural breaks.
var fld=this.getField("PricePaidasapercentofParValueofGuaranteedInterest”).value;
if(fld>110)
{event.value=fld*.5}
else
{event.value=""}
Copy link to clipboard
Copied
If it's any help, adobe gets stuck on this(highlighted in blue when I try to accept the code):
If(fld>110)
Copy link to clipboard
Copied
As I said, you're using non-standard quotes, namely a curly quote after the field name.
Copy link to clipboard
Copied
Sorry, not sure what you mean. I physically typed the " on the keyboard (immediately to the left of the "Enter" key.)
So how would I adjust this To fix it?
Copy link to clipboard
Copied
Nevermind. I retyped it and it did change the format in the display and now seems to be working.
Thanks for the help!
Copy link to clipboard
Copied
What editor did you use for writing the code? Don't use Word or anything like that, but a plain-text editor, where no substitutions can occur.
Copy link to clipboard
Copied
I think that's exactly what happened. As I was copying and pasting into Word, it reformatted the code.
My office doesn't have much, and IT won't let me download any of the free code software....but I'm thinking Notepad from now on.
Thoughts?
Copy link to clipboard
Copied
Notepad is not ideal, but much better than Word... Ask them to install for you Notepad++ for a truly powerful and complete editing tool. It's free and absolutely safe to use, and will make your job much easier if you need to write a lot of code.
For example, it would have helped you identify this error quite easily because you could see that the color of the text doesn't change at the end of the string, as it does when it's properly closed (first line is the bad code you had, second one is the correct version):
Copy link to clipboard
Copied
Ahhh, yes. Interesting.
Well thank you for the help, I'll need to see if I can cash in some chips and pull some strings.
I'm just happy to know I didn't completely botch the code. And such a simple fix, that I'll be sure to look out for in the future!