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

I need help writing a calculation

Community Beginner ,
Jun 30, 2023 Jun 30, 2023

Right now what I have is this, but it isn't working:

     var EXT = this.getField("Warranty").value;
     if (Warranty = Extended) event.value = Number(this.getField("Quantity_Tablets").valueAsString) * 279

 

There are four different varieties of warranties with different prices, and based on clickboxes, I need to get the number multiplied by different values.

 

Please help!

TOPICS
PDF forms
1.3K
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
1 ACCEPTED SOLUTION
Community Expert ,
Jun 30, 2023 Jun 30, 2023

Use this:

if (this.getField("Warranty").valueAsString == "Extended")
event.value = Number(this.getField("Quantity_Tablets").valueAsString) * 279;

View solution in original post

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 Beginner ,
Jun 30, 2023 Jun 30, 2023
 
C.Vongphit
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 ,
Jun 30, 2023 Jun 30, 2023

Where does you set the variables Warranty and Extended?

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 Beginner ,
Jun 30, 2023 Jun 30, 2023

There is a set of radial buttons that are named "Warranty Charges" and the four Radio Button Choices are Standard, Extended, Accidental and Ext+Acc. I have renamed the button, and the field is now Warranty Charges, but it doesn't work either way.

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
LEGEND ,
Jun 30, 2023 Jun 30, 2023

You write

if (Warranty = Extended) 

First, this you make the mistake EVERYONE makes. To test if things are equal, you need two equal signs == not =.

Second, you are comparing a variable called Warranty with a variable called Extended.

You must ALREADY have set Warranty and Extended to something. On the other hand if you want to get the value of a field you have to use this.getField. And if you want to compare with a string, it has to be in quotes. So, conceivably you really meant

if ( this.getField("Warranty") == "Extended" )

but we don't know your intention. Also you don't mention having a field called "Warranty".

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 Beginner ,
Jun 30, 2023 Jun 30, 2023

I've renamed the field to "Warranty Charges" and have changed it in the rule.

 

This is it's current version:

var EXT = this.getField("Warranty Charges").value;
if (this.getField("Warranty Charges") == "Extended") event.value = Number(this.getField("Quantity_Tablets").valueAsString) * 279

 

It doesn't work either. Quantity_Tablets is the total of items ordered, so that's what I want to multiply by the dollar amount.

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 ,
Jun 30, 2023 Jun 30, 2023

You doesn't use the value of the field "Warranty Charges".

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 ,
Jun 30, 2023 Jun 30, 2023

Use this:

if (this.getField("Warranty").valueAsString == "Extended")
event.value = Number(this.getField("Quantity_Tablets").valueAsString) * 279;

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 Beginner ,
Jun 30, 2023 Jun 30, 2023

This almost works. What works is that when I go from Standard to Extended to Accidental to Ext+Acc, it figures the amount. What doesn't is that it just keeps adding them togther, it never goes back to 0 for Extended when another is chosen. Do I need an "else" in there somewhere? If so, how do I write it?

 

Thanks so much!

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 ,
Jun 30, 2023 Jun 30, 2023

Add:

else
event.value = 0;

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 Beginner ,
Jun 30, 2023 Jun 30, 2023

THANK YOU! THANK YOU! THANK YOU!

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 ,
Jun 30, 2023 Jun 30, 2023
LATEST

Use "==" in the if.  "=" is the assignment operator. "==" is the comparison operator.

 

you can read more about writing calculation scripts here:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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