Skip to main content
Participating Frequently
October 19, 2021
Question

Document JavaScript and Validation

  • October 19, 2021
  • 6 replies
  • 1671 views

My Adobe Acrobat was going slow due to the calculations I had. So I moved the calculations into a Document JavaScript. Below is one example of 20 calculation functions. The I go to the Total01 text Field and select run custom Validation Script. In there I have Total01(); However it does not work what am I missing?

function Total01()
{
    // Get the field values, as numbers
    var v1 = this.getField("Cost01").valueAsString;
    var v2 = this.getField("Trips01").valueAsString;

    // Multiply the values
    var result = v1 * v2;

    // Set the value of this field, rounding to two decimal places
    Total01=result;
};

 

This topic has been closed for replies.

6 replies

JR Boulay
Adobe Expert
September 2, 2022

"moving calculations to functions is recommended, because it is easier to maintain. It won't make anything faster if the number of calculations is the same! You have to reduce the number of calculations."

Since each user event triggers all calculations in all calculated fields one after the other, the killer trick is to use a single trigger in only one field that does all the calculations, so that it really goes faster.

 

(Thanks to Thom Parker for this useful tip!)

Acrobate du PDF, InDesigner et Photoshopographe
deaconf19Author
Participating Frequently
September 13, 2022

Yeah I thought I had this working but it slowed back down.

 

I am now trying the below to test the speed. Not sure how to do a single trigger like you mentioned

var v1 = this.getField("Cost01").valueAsString;
var v2 = this.getField("Trips01").valueAsString;


if (v1=="" && v2=="") event.value = "";
else {
	var Cost01 = Number(v1);
	var Trips01 = Number(v2);
	event.value = Cost01 * Trips01;
}

 

deaconf19Author
Participating Frequently
September 1, 2022

After year and revisting this I was able to solve the problem of the constant recalcuation.

 

For the Total filed I went to custom calculation and added the following

// Get value of fields as a number
var n1 = +getField("Cost01").value;
var n2 = +getField("Days01").value;
var n3 = +getField("Trips01").value;
// Set this field's value
event.value = (n1 * n2) * n3;

try67
Adobe Expert
October 19, 2021

Change the last line to:

event.value = result;

 

Also, just moving the calculation scripts from the fields themselves to a doc-level function will not change the speed in which they run.

deaconf19Author
Participating Frequently
October 20, 2021

Just to make sure I wasn't going down a rabbit hole.

 

  1. I cleared all calculations and JavaScript from the form.
  2. Named all fields correctly and uniformed
  3. Reentered the calculations

 

If you start off at Airfare/Train (Tickets/Exchanges/Agent Fees) enter in a cost amount then press tab. You will see it takes a second to calculate. When more data is entered in the fields the responsivness gradually slows. Then adding in digital signatures adds 5 seconds.

 

I thought that if I moved the calculations into a Document JavaScript then put the function into the valdiation it would only run when there is a change. I might be misunderstanding how validation works. I basically only want to calculate with a change.event.

 

try67
Adobe Expert
October 20, 2021

Works fine for me.

Bernd Alheit
Adobe Expert
October 19, 2021

Scripts at validation makes only sense on fields where users enter data.

deaconf19Author
Participating Frequently
October 19, 2021

I only want it to calculate when a user changes the field.

Attached is a screenshot of where 90% of the calculations are occurring.

For each Total and SubTotal field, I had a Calculate field with Value is the product (x) with fields from their respective row as the input.

This worked fine. The problem when you select the next field it takes upwards of 10 seconds for the form to become responsive.

Bernd Alheit
Adobe Expert
October 19, 2021

What field does the user change?

Brainiac
October 19, 2021

PS: moving calculations to functions is recommended, because it is easier to maintain. It won't make anything faster if the number of calculations is the same! You have to reduce the number of calculations.

deaconf19Author
Participating Frequently
October 19, 2021

Thanks for the information. I am not a JavaScript person, so I am unsure how to pass it as a parameter. I pieced milled this together by looking around online and testing.

My problem was with all the calculations. The form was sluggish when filling it out. I read that moving it to a Document JavaScript would speed it up each time you change a field. It refreshes them all no matter what.

I only want to calculate if there is a change in the fields; otherwise, leave it at $0.00

Can you point me in a direction on how to do this? I have been Googling for 5 hours now to no avail.

Brainiac
October 19, 2021

1. It's not safe to use "this" in a function, because the meaning of "this" changes according to where it is run. Pass it as a parameter.

 

2. I think you are expecting

Total01=result;

to do something other than set a variable called Total01. Like set a field? Compare with what you do in the former calculation, before you made a function.

 

3. Why use valueAsString when you want a number?

deaconf19Author
Participating Frequently
October 19, 2021

I changed the valueAsString to value as I realized my mistake on that one.