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

I need help writing a calculation

Community Beginner ,
Jun 30, 2023 Jun 30, 2023

Copy link to clipboard

Copied

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

Views

1.0K

Translate

Translate

Report

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

Copy link to clipboard

Copied

Use this:

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

View solution in original post

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

 
C.Vongphit

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Where does you set the variables Warranty and Extended?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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".

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Use this:

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Add:

else
event.value = 0;

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

THANK YOU! THANK YOU! THANK YOU!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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