custom calculation script

when i put this in, i get the nothing: event.value = ( this.getField("PLRegTotal").value * this.getField("RegPlumberLaborRate").value ) + ( this.getField("HRegTotal").value * this.getField("RegHelperLaborRate").value ) + ( this.getField("PlOTTotal").value * this.getField("OTPlumberLabor").value ) + ( this.getField("HOTTotal").value * this.getField("OTHelperLaborRate").value );

However if I just have this: event.value = ( this.getField("PLRegTotal").value * this.getField("RegPlumberLaborRate").value ) + ( this.getField("HRegTotal").value * this.getField("RegHelperLaborRate").value ); it works. What am I doing wrong?


Tracy Bible


8 Answers

Voted Best Answer

The good news is that now you know which name is the source of the problem.

The field name on the text field and the field name in the script have to be the same and are case sensitive.

OTPlumberLaborRate is different from OTPLumberLaborRate, for example.

Pay attention to the spelling and remember that JavaScript names are case sensitive.

To make sure the names are the same, edit the form (go to menu View -> Tools -> Forms -> Edit), double click the field which name is the problem, copy its name and paste it in the script.

enter image description here


By Almir R V Santos   

By looking at the information in this message, it is very difficult to say what the problem is.

Nevertheless, a few things which may be the cause:

• Are the field names spelled correctly?

• Do you get any error message (in order to get proper error messages, you will have to fully activate the JavaScript Console/Debugger (in the JavaScript tab of the Preferences); i particular the show messages on errors option. You will have to restart Acrobat in order to get this working.

• Are the quotes correct (JavaScript requires straight quotes ( " )?

Hope this can help.

Max Wyss.


Max Wyss   

There is nothing wrong with the syntax, maybe some of the field names are not written correctly and thus not found.

When you use the second script and nothing happens you can open the JavaScript Debugger to see if there is any message. If you get a message such as "this.getField("fieldName") is null" you will know which field name is the problem.

To show the debugger console go to menu View -> Tools -> JavaScript and click on "JavaScript Debugger"

enter image description here

To avoid keep remembering to open the debugger to verify if there are messages you can set Acrobat preferences to show the console whenever a error or message is generated. To do this, go to menu Edit -> Preferences, choose JavaScript category and enable "Show console on errors and messages"

enter image description here


Almir R V Santos   

When you access a field object using ths "this.getField)" method, the method either returns the filed object or a null value. You can test for the null returned value to find the error and not kill the script. And since it pops an alert message when there is a bad field name, one does not need to open the JavaScript console.

// document level function;
function GetField(cName) {
// return the field object for cName or return a null value;
var oField = this.getField(cName);
if(oField == null) app.alert("Error accessing field " + cName), 0, 0);
return oField;
} // end GetField function;

// custom JavaScript field calculation;
event.value = ( GetField("PLRegTotal").value * GetField("RegPlumberLaborRate").value ) + ( GetField("HRegTotal").value * GetField("RegHelperLaborRate").value ) + ( GetField("PlOTTotal").value * GetField("OTPlumberLabor").value ) + ( GetField("HOTTotal").value * GetField("OTHelperLaborRate").value );

Using a document level function lets me use the function anywhere within the form.

If any of the fields used in the script are the result of a calculation, you should check the field calculation order. This is different from reading and tab ordering.

If you can post a link to the form then we might be able to find other errors.

You should be aware that JavaScript is an interpreted language and as soon as there is one error found it stops working. You fix that error, and the next error will stop it dead.


George Kaiser   

From a different question posted by Tracy:

Follow up to last weeks assistance to Adobe Acrobat pro calculation

https://mallickmechanical.box.com/s/ud5pprzagum0rvo50xd5 Any help you could provide would be great. I am stuck!


Karl Heinz Kremer   

When I edit the value of any of the fields in your file this error message appears:

TypeError: f is null

This means there's a problem with one of the scripts. Unfortunately it doesn't say which one... My guess is you used the wrong field name in one of the Simple Field Notation calculation codes. You need to fix it if you want your calculations to work properly.

.


Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

The problem is in the "TotalEst" field. As you have changed the names of the fields used to calculate the Total you have to pick the fields again.

This is how it looks now:

enter image description here

It should look like this:

enter image description here

If you pick the fields again (for the Total field) this will fix the problem.

Once you have fixed that you have to pay attention to the field calculation order, since you have subtotals and total.
Edit your form, and under "Other Tasks" choose "Set Field Calculation Order..." to set the appropriate one:

enter image description here

enter image description here


Almir R V Santos   

You have to analyze the LaborCost field calculation and the calculation order.

The code for LaborCost field can be written like this, to make it easier for reading:

event.value =
    (this.getField("PLREGHOURS").value * this.getField("RegPlumberLaborRate").value) +
    (this.getField("HRegTotal").value * this.getField("RegHelperLaborRate").value) +
    (this.getField("PLOTHOURS").value * this.getField("OTPlumberLaborRate").value) +
    (this.getField("HOTHOURS").value * this.getField("OTHelperLaborRate").value);

For example, PLREGHOURS, HRegTotal, PLOTHOURS and HOTHOURS fields have to be calculated before LaborCost. You have to check the calculation order (I haven't done that for you).
I could realize that the calculation for the fields PLOTHOURS and HOTHOURS is missing , I believe you have to sum the values for their columns, so verify the sum for these fields.


Almir R V Santos   


Please specify a reason: