Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Conditional fee calculation in document level javascript

Explorer ,
Dec 17, 2019 Dec 17, 2019

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

TOPICS
Acrobat SDK and JavaScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 18, 2019 Dec 18, 2019

Place it as the custom calculation script of a (hidden) text field.

Translate
Explorer , Dec 18, 2019 Dec 18, 2019

That worked! Thank you so much for your help!

Translate
Community Expert ,
Dec 17, 2019 Dec 17, 2019

Hi,

In a first step, try with:

...

if (LicenseFee=='') {

ServiceFee='';

...

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 17, 2019 Dec 17, 2019

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 17, 2019 Dec 17, 2019

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 17, 2019 Dec 17, 2019

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 17, 2019 Dec 17, 2019

Into the code you posted above, at the end of the function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 17, 2019 Dec 17, 2019

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;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 17, 2019 Dec 17, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 17, 2019 Dec 17, 2019

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2019 Dec 18, 2019

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2019 Dec 18, 2019

Where does you use/call the function?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2019 Dec 18, 2019

Under Document Javascript

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2019 Dec 18, 2019

Why does you call the function there?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2019 Dec 18, 2019

Adding calculations to the individual fields wasn't getting the result I wanted (see above). Do you have another suggestion?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2019 Dec 18, 2019

Also the three individual fields themselves do not contain any JS, the JS is just at Document JS level

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2019 Dec 18, 2019

When you call the function at document level the calculation will be performed when you open the file.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2019 Dec 18, 2019

That's not the correct location for the JS? What would be the proper way to implement it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2019 Dec 18, 2019

Place it as the custom calculation script of a (hidden) text field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2019 Dec 18, 2019
LATEST

That worked! Thank you so much for your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines