These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Percentage Calc (Y or N) in New Field

lschaffer
Registered: Oct 30 2009
Posts: 5
Answered

I have a form where I've calculated an increase (in percentage) in a worker's pay. I now want to add a field next to it simply indicating "Y" if the increase is >= 3.50% or "N" if it is not. I have some code below that I put together in a Custom Calculation Script in the properties of the percentage field, but it is not working. I am new to Javascript. Can someone have a look at the code to see where I went wrong?? Any help would be greatly appreciated as this project needs to be done ASAP!!! Thanks.
myIncrease is my percentage field and PerYN is my Y or N field.

var myIncrease = getField("Increase");
var myCheck =
getField("PerYN");
if(myIncrease.value >= ".035")
{
myCheck.value = "Y";
} else {
myCheck.value = "N";
}

My Product Information:
Acrobat Pro 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
On the surface there's nothing totally wrong with your script. But there are some very bad coding practices here that are probably getting you in trouble. Here are some things to think about.

1. If you placed this code in the percentage calculation script then it is in the completely wrong place. This code should be in the calculation script for the "PerYN" field.

2. Field event scripts MUST interact with the field through the event object. Explictly aquiring the field object will cause problems. Sometimes fatal ones. For a calculation script the field value is set with "event.value"

3. The comparison in the "if" is numeric, but you've specified a string value. In this context the JavaScript engine will usually do the correct conversion, but this is not guarenteed. It could be doing a lexical rather then a numeric comparison. Do not quote the 0.35 value.

3. Are the field names correct?

4. Is the "PerYN" field a text field? If it is a checkbox field you'll need to use a different methodology.

5. Did you check the JavaScript Console to see if any errors were reported? (See the info on the Console in my signature block)

6. To debug a script you place "console.println()" statements throughtout the code. This is the first thing you should do when things aren't working.

7. Read this article on Calculation scripts:
http://www.acrobatusers.com/tutorials/2006/form_calculations

8. You might also want to watch the videos here:
http://www.pdfscripting.com/public/34.cfm#FirstForm


Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

lschaffer
Registered: Oct 30 2009
Posts: 5
Your suggestions helped immensely!! With a couple changes to the code, it's working fine. Thanks a lot. Learning more as I go. :)