19 Replies Latest reply on Sep 22, 2014 6:59 AM by bruce00j

    Javascript calculations not resolving properly

    bruce00j Level 1

      Hi,

       

      I am currently making a PDF that makes labels for products based on what is entered on the first page.

       

      I have been running it ok for a while and just printing off tests, now out of no where after no changes some of the calculations are not resolving.

       

      For example I have a row of coloured tick boxes to show what colour the product is, on the label it is typed out twice once in an abbreviation for a product code then in full for the description.

       

      I have noticed that now some of the colours won't register with the label until you check the next one and sometimes it will be delayed by 1.

       

      I have come across this problem before and was unable to fix it, I have tried chaining the calculation sequence but to no avail.

       

      Is there anything else I can do to fix this?

       

      Thanks,

      Bruce

        • 1. Re: Javascript calculations not resolving properly
          gkaiseril MVP & Adobe Community Professional

          If you are using any calculations, check the field calculation order. The calculation order is not the same as the field order. Also one must exit a field before that calculation or on blur action will be triggered.

           

          A link to the form with the problem would be helpful.

          • 2. Re: Javascript calculations not resolving properly
            bruce00j Level 1

            I have tried to change the calculation field order around and still no avail.

             

            I shall PM you a link to the form when I get it uploaded somewhere

             

            Thanks,

            Bruce

            • 3. Re: Javascript calculations not resolving properly
              gkaiseril MVP & Adobe Community Professional

              I would start by simplifying the form to just the form fields you need to gather the information and drop all the hidden fields that are just changing values. Then using just the basic fields work out the computed fields. You can easily change the case of the field and combine values as part of the calculation to create the labels.

              • 4. Re: Re: Javascript calculations not resolving properly
                bruce00j Level 1

                Hi so I started to simplify the size variable for the product code with

                if(this.getField("Size").value=='SMALL'){var three = "SM"
                } else if (this.getField("Size").value=='MEDIUM'){ var three = "ME"
                } else if (this.getField("Size").value=='STANDARD'){ var three = "ST"
                } else if (this.getField("Size").value=='LARGE'){ var three = "LG"
                } else { value = "?"
                }
                

                when I go back to my form it is saying that it is undifiend and won't change when I change what is in the field "Size".

                 

                Any reason as to why this could be?

                 

                Thanks,

                Bruce

                • 5. Re: Javascript calculations not resolving properly
                  try67 MVP & Adobe Community Professional

                  The last else statement doesn't seem correct. Also, if this is the custom

                  calculation script of the "Size" field then you should use event.value

                  instead of accessing the field using getField.

                  • 6. Re: Javascript calculations not resolving properly
                    bruce00j Level 1

                    Oh, erm I'm not sure how it is wrong could you help? and it is not in the "Size" field. The "Size" field will contain SMALL, MEDIUM, STANDARD or LARGE and then it relays to a product code field which abbreviates it to SM, ME, ST or LG I have tried to remove the need for another field to change it yet I can't seem to get the product field to set a variable based on the size field and abbreviate it.

                     

                    Thanks,

                    Bruce

                    • 7. Re: Javascript calculations not resolving properly
                      try67 MVP & Adobe Community Professional

                      Yes, I see. In that case, use this code as that field's custom calculation code:

                       

                      if (this.getField("Size").value=='SMALL'){event.value = "SM"}

                      else if (this.getField("Size").value=='MEDIUM'){ event.three = "ME"}

                      else if (this.getField("Size").value=='STANDARD'){ event.three = "ST"}

                      else if (this.getField("Size").value=='LARGE'){ event.three = "LG"}

                      else { event.value = "?" } 

                      • 8. Re: Re: Javascript calculations not resolving properly
                        bruce00j Level 1

                        Hi,

                         

                        Sorry for the late reply.

                         

                        I had ago at this when I saw it but I seem to be getting the wrong output from it and I am not sure as to why this is.

                         

                        I am using the colours as I have found that only the colours seem to not work properly.

                         

                        if(this.getField("Colour").value=='MB'){
                        event.four = "MEDICAL BLUE"
                        } else if (this.getField("Colour").value=='LB'){
                        event.four = "LIGHT BLUE"
                        } else if (this.getField("Colour").value=='CR'){
                        event.four = "CREAM"
                        } else if (this.getField("Colour").value=='GR'){
                        event.four = "GREY"
                        } else if (this.getField("Colour").value=='MG'){
                        event.four = "MEDICAL GREEN"
                        } else if (this.getField("Colour").value=='LI'){
                        event.four = "LILAC"
                        } else {
                        event.four = "?"
                        }
                        
                        

                        I seem to only get the abbreviated text as a result and not what I defined and reason why this is?

                         

                        I am using radio buttons for the colour if this makes it any different?

                         

                        Thanks,

                        Bruce

                        • 9. Re: Re: Javascript calculations not resolving properly
                          gkaiseril MVP & Adobe Community Professional

                          What is the "event" "four" property?

                           

                          This property is not documented in the Acrobat JavaScript API Reference.

                          • 10. Re: Javascript calculations not resolving properly
                            bruce00j Level 1

                            Hi,

                             

                            In Gilad's code he uses event.three which I am guess is defining the "three" variable and in my code I am wanting to define a variable "four"

                             

                            Thanks,

                            Bruce

                            • 11. Re: Javascript calculations not resolving properly
                              gkaiseril MVP & Adobe Community Professional

                              Please re-read the script.

                               

                              He is setting the "value" property of the field object.

                               

                              if (this.getField("Size").value=='SMALL'){event.value = "SM"}

                              else if (this.getField("Size").value=='MEDIUM'){ event.three = "ME"}

                              else if (this.getField("Size").value=='STANDARD'){ event.three = "ST"}

                              else if (this.getField("Size").value=='LARGE'){ event.three = "LG"}

                              else { event.value = "?" } 

                              • 12. Re: Javascript calculations not resolving properly
                                bruce00j Level 1

                                From my understanding event.value sets the value of the full field, what I am trying to do is set the value of a variable not the flu field is that is the case.

                                 

                                Or would it be

                                 

                                var three = if (this.getField("Size").value=='SMALL'){event.value="SM"}

                                else if (this.getField("Size").value=='MEDIUM'){ event.three = "ME"}...

                                 

                                Thanks,

                                Bruce

                                • 13. Re: Javascript calculations not resolving properly
                                  gkaiseril MVP & Adobe Community Professional


                                  Variables are different than objects and they only have a value.

                                   

                                  If you are just filling field values with the value of other fields just get the value of the other fields.

                                   

                                  I usually use variables to accumulate information like a sum or count to be used in a calculation.

                                  • 14. Re: Javascript calculations not resolving properly
                                    bruce00j Level 1

                                    I have been doing that but I have had to use different field to get it abbreviated but it has caused errors in the calculation order and causes delays or invalid inputs in to some of the fields and I have been advised to make it simpler by calculating it within one field but I am having trouble abbreviating or lengthening some values into a variable.

                                     

                                    Thanks,

                                    Bruce

                                    • 15. Re: Javascript calculations not resolving properly
                                      gkaiseril MVP & Adobe Community Professional


                                      Then you have to track down each error and figure out how to fix it. The JavaScript console should tell you the type of error, the field involved or the script and the line number.

                                       

                                      By simplify I meant get rid of the unnecessary fields and rewrite your scripts to use only the visible fields.

                                      • 16. Re: Javascript calculations not resolving properly
                                        bruce00j Level 1

                                        I have found no errors at all in the java debug console only that when I click on a colour the first time round it says that a variable in the description field is not defined but it won't repeat that when I continue to use it and the value does change but it won't change to the correct value.

                                         

                                        Thanks,

                                        Bruce

                                        • 17. Re: Javascript calculations not resolving properly
                                          Bernd Alheit Adobe Community Professional & MVP

                                          Gilad D (try67) wrote:

                                           

                                          Yes, I see. In that case, use this code as that field's custom calculation code:

                                           

                                          if (this.getField("Size").value=='SMALL'){event.value = "SM"}

                                          else if (this.getField("Size").value=='MEDIUM'){ event.three = "ME"}

                                          else if (this.getField("Size").value=='STANDARD'){ event.three = "ST"}

                                          else if (this.getField("Size").value=='LARGE'){ event.three = "LG"}

                                          else { event.value = "?" }

                                          What is event.three ?

                                          • 18. Re: Javascript calculations not resolving properly
                                            try67 MVP & Adobe Community Professional

                                            Sorry, I copied it over and didn't change it properly. They should all be event.value, of course.

                                            • 19. Re: Javascript calculations not resolving properly
                                              bruce00j Level 1

                                              I don't seem to be getting anywhere with this now.

                                               

                                              Is there a way to set a variable through an if statement as this is what I am needing else I need and extra text field to change it and then I end up back in square one with my colour field not working correctly at all.

                                               

                                              Basically what I need is to be able to get 2 values from radio buttons.

                                               

                                              The current radio buttons give out

                                              MB

                                              LB

                                              GR

                                              MG

                                              LI

                                              GR

                                               

                                              and they all go well into the product code but i need to be able to have them in the description as

                                              MEDICAL BLUE

                                              LIGHT BLUE

                                              GREY

                                              MEDICAL GREEN

                                              LILAC

                                              GREY

                                               

                                              I have tried to change around the calculation order but I don't understand it what so ever and no matter what I try I still end up with the same problem.

                                               

                                              I have been stuck with this issue too long now and I have no other option at the minuet I tried applescript but I still need the first page of the this PDF to be filled out without more user input.

                                               

                                              Thanks,

                                              Bruce