Highlighted

JS calculations are slow

Explorer ,
Nov 03, 2020

Copy link to clipboard

Copied

Hi, 

I made my first form and everything is working good. That was my first time creating a form and my first try with JS. I know thatmy calculations are porbably not optimal, but i'd like to figure out something without starting all over or make dramatic changes to my form. 

I'd like to know if there is a way for my calculations to be faster. 

There is a little delay when choosing an item in many fields but specifically in those fields :

Type de collage

Produit

I can see that the calculations are runing. It could be anoying for the customer to wait everytime he choose an item.

If someone has a clue for me it would be appreciated.

I am working on Adobe Acrobat Pro DC.

My form is in french. 

Here is the link to my form :

https://documentcloud.adobe.com/link/review?uri=urn:aaid:scds:US:2c98e0b3-8af5-4701-972d-85991c54ca4...

Thanks, 

Kassandra 

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Good for you!! It's well worth the effort to try different ways to script a behavior, even if it doesn't work out. However, since setting the  "Dessin" depends on several inputs, it is appropiate to use a Calculation Script, just not on the dropdown. Instead, put a new text field next to the "Dessin" checkbox, and use the calculation script on this text box. Make this text field hidden. This separates the code for the calculation from the source fields. 

 

Now you need to learn a bit about how to write calculations.

These short article can help:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

In this case the calculation isn't with numbers, but with boolean values.  

Here's the calculation script for the hidden text field next to the "Dessin" checkbox

 

 

var bDessinValue = (this.getField("Decoupe").valueAsString != "0")
                      || (this.getField("Assemblagedessus").valueAsString != "0") 
                      || (this.getField("Assemblagepattes").value != 0)
                      || (this.getField("Moulurage").value =="Personnalisé") 
                      || (this.getField("Porteafaux") == "Oui");

this.getField("Dessin").value = bDessinValue?"Yes":"Off";

 

 

 

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

107

Likes

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

JS calculations are slow

Explorer ,
Nov 03, 2020

Copy link to clipboard

Copied

Hi, 

I made my first form and everything is working good. That was my first time creating a form and my first try with JS. I know thatmy calculations are porbably not optimal, but i'd like to figure out something without starting all over or make dramatic changes to my form. 

I'd like to know if there is a way for my calculations to be faster. 

There is a little delay when choosing an item in many fields but specifically in those fields :

Type de collage

Produit

I can see that the calculations are runing. It could be anoying for the customer to wait everytime he choose an item.

If someone has a clue for me it would be appreciated.

I am working on Adobe Acrobat Pro DC.

My form is in french. 

Here is the link to my form :

https://documentcloud.adobe.com/link/review?uri=urn:aaid:scds:US:2c98e0b3-8af5-4701-972d-85991c54ca4...

Thanks, 

Kassandra 

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Good for you!! It's well worth the effort to try different ways to script a behavior, even if it doesn't work out. However, since setting the  "Dessin" depends on several inputs, it is appropiate to use a Calculation Script, just not on the dropdown. Instead, put a new text field next to the "Dessin" checkbox, and use the calculation script on this text box. Make this text field hidden. This separates the code for the calculation from the source fields. 

 

Now you need to learn a bit about how to write calculations.

These short article can help:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

In this case the calculation isn't with numbers, but with boolean values.  

Here's the calculation script for the hidden text field next to the "Dessin" checkbox

 

 

var bDessinValue = (this.getField("Decoupe").valueAsString != "0")
                      || (this.getField("Assemblagedessus").valueAsString != "0") 
                      || (this.getField("Assemblagepattes").value != 0)
                      || (this.getField("Moulurage").value =="Personnalisé") 
                      || (this.getField("Porteafaux") == "Oui");

this.getField("Dessin").value = bDessinValue?"Yes":"Off";

 

 

 

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

108

Likes

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
Nov 03, 2020 0
Adobe Community Professional ,
Nov 03, 2020

Copy link to clipboard

Copied

Calculations are problematic because they are triggered every time any field value is changed, which means they are usually run many more times than necessary. In fact, it's quite easy to create huge cascades of unnecessary calculations. 

The solution is to reduce the number of calculation scripts. There are several methods for fixing this issue. 

1) Move anything that doesn't need to be a calculation into a different type of script. The obvious targets are any calculation that only depends on one input. This can probably be moved to a validation script on the source field. 

2) Test for the source of a calculation before proceeding. If the source is not one of the inputs to the calc, then don't run the code.

3) Make sure your calculations are not spawning other calculations. This happens when a calculation script directly changes a field other than itself.  If you have calculations that do this, then it should probably be done a different way. 

Likes

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
Reply
Loading...
Nov 03, 2020 1
Adobe Community Professional ,
Nov 03, 2020

Copy link to clipboard

Copied

BTW: sharing you document as a review is problematic. If you want us to examine it, it should be shared as a plain file. 

Likes

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
Reply
Loading...
Nov 03, 2020 0
Explorer ,
Nov 03, 2020

Copy link to clipboard

Copied

Thanks for the advices.

I just didn't know how to share it plain.

Likes

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
Reply
Loading...
Nov 03, 2020 0
Adobe Community Professional ,
Nov 03, 2020

Copy link to clipboard

Copied

You should be able to add it to this thread by editing your first post. The edit will include a paper clip symbol for attaching a file. 

Likes

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
Reply
Loading...
Nov 03, 2020 0
Explorer ,
Nov 04, 2020

Copy link to clipboard

Copied

I see no options to edit my first post....

Likes

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
Reply
Loading...
Nov 04, 2020 0
Adobe Community Professional ,
Nov 04, 2020

Copy link to clipboard

Copied

If there isn't a "... More" link under the post, then you could share the document through Google Docs or DropBox.

 

Likes

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
Reply
Loading...
Nov 04, 2020 0
Explorer ,
Nov 04, 2020

Copy link to clipboard

Copied

Could you tell me if this document is ok for you to look at it ?

https://drive.google.com/file/d/1j2U7qkGuP-cSp1ceDRQqoXzqjTWkYBWq/view?usp=sharing

Thanks

Likes

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
Reply
Loading...
Nov 04, 2020 0
Adobe Community Professional ,
Nov 04, 2020

Copy link to clipboard

Copied

You should also fix the script errors in the form. 

Likes

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
Reply
Loading...
Nov 04, 2020 1
Explorer ,
Nov 04, 2020

Copy link to clipboard

Copied

I don't know how to do that though.

It's my first time using JS, so I probably don't know how to use it correctly yet.

Likes

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
Reply
Loading...
Nov 04, 2020 0
Adobe Community Professional ,
Nov 04, 2020

Copy link to clipboard

Copied

Look at the JS console for the error messages. 

Likes

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
Reply
Loading...
Nov 04, 2020 1
Adobe Community Professional ,
Nov 04, 2020

Copy link to clipboard

Copied

You'll find a video on how to use the Console Window here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

You should also read this page:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

The main error is a bad field name, used in "this.getField" in one of the calculation scripts.

A major problem with form is that most of your calculations should not be calculations. From what I can see, every calculation script used on on a dropdown, should be a validation script. Fixing this would probably fix the slowness.  

Likes

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
Reply
Loading...
Nov 04, 2020 1
Explorer ,
Nov 04, 2020

Copy link to clipboard

Copied

Thanks a lot. Can you tell me which field name is in error ? 

I'll have a look on the links you just send.

Really appreciate your help.

Likes

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
Reply
Loading...
Nov 04, 2020 0
Adobe Community Professional ,
Nov 04, 2020

Copy link to clipboard

Copied

That is an exersise left to the user 😉

 

I would suggest moving all the calculation scripts on the dropdowns to validation scripts. Then it will become apparent (in the console) when you do a selection.

 

Field names must be verbatim in every place they are used.  

Likes

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
Reply
Loading...
Nov 04, 2020 0
Most Valuable Participant ,
Nov 04, 2020

Copy link to clipboard

Copied

In the newer versions of Acrobat the error message doesn't include the name of the field you were trying to access that caused the error, just "...", which makes it incredible difficult to locate the issue, since the name of the field that triggered it is also not included. Luckily, I have access to an older version so I can help you locate the issues. Here's the full text of the error messages I'm getting from the file you shared:

 

TypeError: this.getField("P" + (this.numPages - 1) + ".JONH" + ".Typedecollage6") is null
1:Field:Calculate
TypeError: this.getField("P" + (this.numPages - 1) + ".JONH" + ".Typedecollage5") is null
1:Field:Calculate
TypeError: this.getField("P" + (this.numPages - 1) + ".JONH" + ".Porteafaux4") is null
1:Field:Calculate
TypeError: this.getField("P" + (this.numPages - 1) + ".JONH" + ".Porteafaux5") is null
1:Field:Calculate
TypeError: this.getField("P" + (this.numPages - 1) + ".JONH" + ".Porteafaux6") is null
1:Field:Calculate
TypeError: this.getField("P" + (this.numPages - 1) + ".JONH" + ".Typedecollage4") is null
1:Field:Calculate

Likes

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
Reply
Loading...
Nov 04, 2020 0
Most Valuable Participant ,
Nov 04, 2020

Copy link to clipboard

Copied

(these fields were supposed to be on a page spawned from the "JONH" template, but it hasn't been spawed yet...)

Likes

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
Reply
Loading...
Nov 04, 2020 0
Explorer ,
Nov 05, 2020

Copy link to clipboard

Copied

Thanks a lot ! I'll try to solve this error.

Likes

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
Reply
Loading...
Nov 05, 2020 0
Explorer ,
Nov 05, 2020

Copy link to clipboard

Copied

So, I was able to switch a part of my calculations into validations scripts but there is one remaining that I don't know how to figure out.

 

This is the calculation of the "Découpe" field.

Basically, what I wanted from that calculation is to check the box " Dessin" when one of those field is not empty or when a specific radio button is at yes. 

If field Découpe or Assemblage-Dessus or Assemblage pattes is not empty

OR if Porteafaux is checked at YES

OR if field Moulurage value is Personnalisé

Then I want my Dessin checkbox to be checked

 

I don't know how to do it differently by using validation script in each field. Every try I made didn't work out well. My value was cleared by the choice made in my second drop down. Even if Decoupe field was not empty, when I cleared field Assemblage-Dessus, it just cleared Dessin box, but my field Decoupe was still filled.

More, I don't know how to add my radio button into that condition.

Wondering if you could help me again, 

Thanks 

 

Likes

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
Reply
Loading...
Nov 05, 2020 0
Adobe Community Professional ,
Nov 05, 2020

Copy link to clipboard

Copied

Good for you!! It's well worth the effort to try different ways to script a behavior, even if it doesn't work out. However, since setting the  "Dessin" depends on several inputs, it is appropiate to use a Calculation Script, just not on the dropdown. Instead, put a new text field next to the "Dessin" checkbox, and use the calculation script on this text box. Make this text field hidden. This separates the code for the calculation from the source fields. 

 

Now you need to learn a bit about how to write calculations.

These short article can help:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

In this case the calculation isn't with numbers, but with boolean values.  

Here's the calculation script for the hidden text field next to the "Dessin" checkbox

 

 

var bDessinValue = (this.getField("Decoupe").valueAsString != "0")
                      || (this.getField("Assemblagedessus").valueAsString != "0") 
                      || (this.getField("Assemblagepattes").value != 0)
                      || (this.getField("Moulurage").value =="Personnalisé") 
                      || (this.getField("Porteafaux") == "Oui");

this.getField("Dessin").value = bDessinValue?"Yes":"Off";

 

 

 

Likes

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
Reply
Loading...
Nov 05, 2020 1
Explorer ,
Nov 05, 2020

Copy link to clipboard

Copied

Thanks a lot again. 

I read the articles but since I am not a member of this website, I can't read all the information. 

But I think I'll figure something out with the code you provided. 

 

Likes

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
Reply
Loading...
Nov 05, 2020 0
Explorer ,
Nov 05, 2020

Copy link to clipboard

Copied

It is working great except for the Porteafaux radio button. This part of the code don't seam to work. 

Even if I checked Oui, the Dessin box is not checking.

Likes

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
Reply
Loading...
Nov 05, 2020 0
Adobe Community Professional ,
Nov 05, 2020

Copy link to clipboard

Copied

If you look carefully you'll see there is an error in the code. The line of code that tests the Porteafaux field does not acquire the "value" property. Here's the correction.

 

|| (this.getField("Porteafaux").value == "Oui");

 

Likes

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
Reply
Loading...
Nov 05, 2020 1
Explorer ,
Nov 09, 2020

Copy link to clipboard

Copied

Thanks a lot. It works perfectly.

I'm grateful you helped me with my project. 

Likes

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
Reply
Loading...
Nov 09, 2020 0