Skip to main content
Participant
November 2, 2023
Question

Calculations Multiplying When I Enter A Value Into A Different Field

  • November 2, 2023
  • 2 replies
  • 3162 views

So, I've been working on this form for call notation for our company. Most of fields are just entry fields for customer information and drop-downs for certain types of customers/calls. The issue I'm running into is on the bottom, where I am trying to calculate how much a transport would cost based on the options chosen in earlier fields. I can't even get through the mileage part yet, let alone the total estimate cost that would be using conditionals. The problem I'm having is that I put the code into the "Custom Calculation Script" under the "Calculate" tab. I am doing this is multiple fields, but changing the field name and calculation each time, but when I go to test the calculations. The first field I try works great, then I go to the second field to calculate a second number and calculates the value again, this time by the newer value. So, for example, I've got a "Mileage" field that multiplies the field entry by 3.25 (aka Cost Per Mile). I have used three different types of code for this and all have the same issue:

 

event.value = event.target.value * 3.25;

event.value = getField("Mileage").value * 3.25;

event.value = this.getField("Mileage").value * 3.25;

 

Say I enter the value 19 into this field. It will calculate it to 61.75, which is correct. But then, I will move on to another field and enter a value. As soon as I hit Enter or Tab, it will calculate that second field, but will also calculate this first Mileage field again. Resulting in an output of 200.69 (I have a number format on both fields so that it rounds to the hundreth place instead of having a long decimal).

 

I'm super confused with this and it's getting frustrating because I cannot move onto the even more complicated conditional calculations and whenever I research this issue, all the solutions are telling me to do exactly this code I have already tried.

 

Any help is greatly appreciated! Note that I am using Adobe Acrobat Pro and am into the "Prepare a form" editor.

This topic has been closed for replies.

2 replies

JR Boulay
Community Expert
Community Expert
November 2, 2023

Why make things complicated when you can make them simple?

 

Acrobate du PDF, InDesigner et Photoshopographe
Participant
November 2, 2023

I have tried this and it also does the same thing. I think the main issue is that I am trying to calculate with the same field that the calculation is in. I've done this in excel and using programming languages such as Java before and it worked just fine, but I suppose I have been spoiled with the ability to tell it how many times I want the code ran instead of it re-running ever time any field is being changed.

Thom Parker
Community Expert
Community Expert
November 2, 2023

You can't do this:

 

event.value = event.target.value * 3.25;

 

Calculations are triggered anytime any field is changed. So this line used as a calculation just continually recalculates itself.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
November 2, 2023

Oh, okay. How about event.value = getField("Mileage").value * 3.25; and event.value = this.getField("Mileage").value * 3.25;?

Do they always go into a neverending calculation when you use the same field the code is in in that calculation, or is there a different code that I can use to get around it becoming cyclical.

try67
Community Expert
Community Expert
November 2, 2023

This code is fine, unless you're applying it to the Mileage field...