14 Replies Latest reply on Jan 29, 2016 5:44 AM by gkaiseril

    JavaScript for refreshing fields?

    TelcoCook

      Hello. I have a form with question fields. I would like to populate several other fields with the answer. The question's answer text field is named "text1" and whatever text is filled in "text1" will also populate in "text2" thru "text8" fields. I found the code below in this forum and modified it to fill in all seven other locations. I placed this code in 'Text Field Properties', 'Calculate' tab, 'Custom calculation script:'.

       

      All works well if all the fields are in the same PDF... I extracted the question page as a separate PDF with the "text1" field (Let's call this the "question" PDF) and kept all the other fields in the "answer" PDF. If I placed a test answer in the "text1" field, saved, then inserted that page into the "answer" PDF the other seven text fields do not get the value of "text1" unless I retype in "text1" field or backspace a few characters and retype then hit enter.

       

      Is there JavaScript that I can place on a button that will refresh all the text fields so I will not need to retype? (the "question" PDF will have close to 50 questions so that would be a hassle). Thank you.

       

      // Populate the other field with this field's value

      getField("text2").value = event.value;

      getField("text3").value = event.value;

      getField("text4").value = event.value;

      getField("text5").value = event.value;

      getField("text6").value = event.value;

      getField("text7").value = event.value;

      getField("text8").value = event.value;

        • 1. Re: JavaScript for refreshing fields?
          gkaiseril MVP & Adobe Community Professional

          So it appears you want Acrobat's JavaScript to detect the missing field and fix the name. Well it is possible for JavaScript to trap the error of the missing field.

           

          Normally JavaScript stops for the entire form when a critical JavaScript error is found. One can use the "try...catch" block of code to trap the error or test the result of the getField method result for its documented return value.

           

          I have posted numerous times a function that uses a field name as an input parameter and returns the field object for found fields and a null value if no field with the provided name was not found. The function also issues an alert about the missing field. Because the error has been identified for the field the scripts will continue to run and sets the expected field object to a null value. One can test for this null value and when found branch around the script trying to access this missing field.

           

          It could be possible to use JavaScript to read each field name in the document and try to figure which existing field was intended to be used.

           

          I would use the On Blur action and not the calculation action, since the On Blur action would only execute when the text1 field is updated. Using the calculate option will trigger the calculation script for Text1 each time any field is used in a calculation.

          • 2. Re: JavaScript for refreshing fields?
            TelcoCook Level 1

            Hello GKaiseril. I should have mentioned in my post that I am a novice when it comes to JavaScript. I'm not quite sure what you mean by "missing field". All the fields are named properly and in the document. I am guessing that you are referring to the fact that when I insert the PDF page with the "text1" field into the PDF with the "text2" thru "text8" that JavaScript does not find "text2" thru "text8" until I retype some characters in the "text1" field.

             

            I will try moving the JavaScript to the "Actions" section and use the On Blur. I will report back this evening.

             

            Thank you!

            • 3. Re: JavaScript for refreshing fields?
              George_Johnson MVP & Adobe Community Professional

              The script should not be a custom calculation script for text1, but rather a custom validate script. It should work as you want after you make this change.

              • 4. Re: JavaScript for refreshing fields?
                lev.savranskiy Level 1

                attach doclevel script with one line

                 

                calculateNow()

                • 5. Re: JavaScript for refreshing fields?
                  TelcoCook Level 1

                  Hello lev.savranskiy. Where should I place that calcualteNow() script?

                  Thank you!

                  • 6. Re: JavaScript for refreshing fields?
                    lev.savranskiy Level 1

                    doclevel  as i said

                    temp2.jpg

                    • 7. Re: JavaScript for refreshing fields?
                      TelcoCook Level 1

                      Thank you for the reply lev.savranskiy.

                       

                      As I am a novice with setting up scripting in a form, you have been very helpful with sending a screen shot to simplify it for me. I will do as you suggest this evening and report back later.

                       

                      Thank you again!

                      • 8. Re: JavaScript for refreshing fields?
                        TelcoCook Level 1

                        Hello again lev.savranskiy.

                        I just found out that since I have Acrobat XI standard, I do not have the document level javaScript. Can you recommend a solution for me to add this capability?

                         

                        Thank you again!

                        • 9. Re: JavaScript for refreshing fields?
                          Karl Heinz Kremer Adobe Community Professional

                          You can use document level scripts even with Acrobat Standard, it's just a bit more complicated to get them into your document: Document Level Scripts With Acrobat Standard - KHKonsulting LLC

                          • 10. Re: JavaScript for refreshing fields?
                            gkaiseril MVP & Adobe Community Professional

                            One can also add document level scripts by inserting a PDF with the document level script and then deleting the inserted pages. The document level script will remain.

                             

                            One can even add the function that traps fields not found by the calculation script by including it the calculation.

                            • 11. Re: JavaScript for refreshing fields?
                              TelcoCook Level 1

                              Thank you gkaiseril. It sounds like from what you are saying, if I was to use khkremer's tool for adding document level scripts, that other folks who use the form I create will still be loading the document level script without needing to install the tool themselves. I like the suggestion of just inserting a page with the doc level script then deleting it. I will ask some others in my group if they have Pro version.

                               

                              This thread has become somewhat diluted (or just my focus has). I will start back from the top and experiment with a few items to see which one works. Too bad I can't just insert a button that would "reload" or "refresh" the javaScript for all form field items. That would be the most user freindly.

                               

                              I will mark "Correct Answer" in this thread as soon as I finish the experiments. Feel free to keep posting ideas.

                               

                              Thank you all.

                              • 12. Re: JavaScript for refreshing fields?
                                lev.savranskiy Level 1

                                you can put script on folder level if you wish.C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Javascripts

                                 

                                but all users have to do the same later

                                • 13. Re: JavaScript for refreshing fields?
                                  gkaiseril MVP & Adobe Community Professional

                                  The script provided in this discussion might have an issue by not providing the value for a document object or using the "this" value for the document object.

                                   

                                  You folder reference is for Acrobat XI. The path for the folder level scripts was changed with version 10.01. I am sure there are many users still using version 8 and 9 and possibly even older.

                                  • 14. Re: JavaScript for refreshing fields?
                                    gkaiseril MVP & Adobe Community Professional

                                    This is one of many ways to add a document level script. th

                                     

                                    One can even create an FDF file to insert document level scripts into a PDF with Acrobat. It is even possible to add a folder level script that can add a button and script to Reader to import an FDF file into Reader and use that new tool to add a document level script with Reader.