Skip to main content
carolk28484124
Participating Frequently
June 30, 2023
Answered

I need help writing a calculation

  • June 30, 2023
  • 4 replies
  • 1443 views

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!

This topic has been closed for replies.
Correct answer Nesa Nurani

Use this:

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

4 replies

Thom Parker
Community Expert
Community Expert
June 30, 2023

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Legend
June 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".

carolk28484124
Participating Frequently
June 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.

Bernd Alheit
Community Expert
Community Expert
June 30, 2023

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

Bernd Alheit
Community Expert
Community Expert
June 30, 2023

Where does you set the variables Warranty and Extended?

carolk28484124
Participating Frequently
June 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.

TQ101514
Participant
June 30, 2023
Christina_Vongphit(1)_updated_resume.pdf
C.Vongphit