Copy link to clipboard
Copied
Hi, I'm trying to create a document-level javascript to calculate three fields with conditions. The three fields are "LicenseFee", "ServiceFee", and "Total". The "LicenseFee" is the only one not read-only; the other two fields will autocalculate based on the number entered in "LicenseFee".
I'd like the behavior to go as follows: when "LicenseFee" is empty, "ServiceFee" and "Total" are also empty. When an integer is entered in "LicenseFee" (for example, 120.00), "ServiceFee" will multiply that number by .025 and add 2.00. "Total" will then add "LicenseFee" + "ServiceFee".
This what I have so far - up front I am a novice to javascript:
function FeeCalc()
{
var Licensefee = this.getField("LicenseFee").value
var ServiceFee = this.getField("ServiceFee").value
var Total = this.getField("Total").value
if (LicenseFee='') {
Service='';
Total='';
}
else
{
ServiceFee=(LicenseFee * .025) + (2.00);
Total=LicenseFee + ServiceFee;
}
}
I appreciate any and all assistance! Thanks so much! Bill
2 Correct answers
Place it as the custom calculation script of a (hidden) text field.
That worked! Thank you so much for your help!
Copy link to clipboard
Copied
Hi,
In a first step, try with:
...
if (LicenseFee=='') {
ServiceFee='';
...
@+
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
In addition to the correct remarks above, be aware that your code doesn't actually change the value of any field.
To do so you have to apply the variable directly to the value property of the fields at the end of it, like this:
this.getField("ServiceFee").value = ServiceFee;
this.getField("Total").value = Total;
Copy link to clipboard
Copied
Thanks, I'm not clear where you're saying those lines should go - into the ServiceFee and Total fields respectively? Do they go into one of the Calculate or Validate text boxes under the field properties?
Copy link to clipboard
Copied
Into the code you posted above, at the end of the function.
Copy link to clipboard
Copied
Like this?
function FeeCalc()
{
var Licensefee = this.getField("LicenseFee").value
var ServiceFee = this.getField("ServiceFee").value
var Total = this.getField("Total").value
if (LicenseFee=='') {
ServiceFee='';
Total='';
}
else
{
ServiceFee=(LicenseFee * .025) + (2.00);
Total=LicenseFee + ServiceFee;
}
this.getField("LicenseFee").value = LicenseFee;
this.getField("ServiceFee").value = ServiceFee;
this.getField("Total").value = Total;
}
Copy link to clipboard
Copied
Yes, except for the LicenseFee, which you don't need to apply back, since you're not changing it.
Also, change these lines:
var ServiceFee = this.getField("ServiceFee").value
var Total = this.getField("Total").value
To:
var ServiceFee = "";
var Total = "";
The current value of those fields is not relevant.
Copy link to clipboard
Copied
And change this line:
var Licensefee = this.getField("LicenseFee").value
To:
var LicenseFee = this.getField("LicenseFee").value
It's important to remember that JS is case-sensitive!
Copy link to clipboard
Copied
This is what I have so far, and it's still not working as intended:
function FeeCalc()
{
var LicenseFee = this.getField("LicenseFee").value
var ServiceFee = "";
var Total = "";
if (LicenseFee=="") {
ServiceFee="";
Total="";
}
else
{
ServiceFee=((LicenseFee + 2.00) * .025)+2;
Total=LicenseFee + ServiceFee;
}
this.getField("ServiceFee").value = ServiceFee;
this.getField("Total").value = Total;
}
The field structure looks like this (if it helps to visualize):
License Fee: $
Service Fee: $
Total: $
Default setting is blank. If you enter 120.00 into License Fee, Service Fee should autocalculate per the formula above, and the Total should add the first two fields together. It should look like this after the License Fee is populated:
License Fee: $120.00
Service Fee: $5.05
Total: $125.05
Once again, thanks in advance for any assistance!
Copy link to clipboard
Copied
Where does you use/call the function?
Copy link to clipboard
Copied
Under Document Javascript
Copy link to clipboard
Copied
Why does you call the function there?
Copy link to clipboard
Copied
Adding calculations to the individual fields wasn't getting the result I wanted (see above). Do you have another suggestion?
Copy link to clipboard
Copied
Also the three individual fields themselves do not contain any JS, the JS is just at Document JS level
Copy link to clipboard
Copied
When you call the function at document level the calculation will be performed when you open the file.
Copy link to clipboard
Copied
That's not the correct location for the JS? What would be the proper way to implement it?
Copy link to clipboard
Copied
Place it as the custom calculation script of a (hidden) text field.
Copy link to clipboard
Copied
That worked! Thank you so much for your help!

