10 Replies Latest reply on Aug 28, 2014 2:28 PM by Maronberg

    Calculation Script Question

    Maronberg

      Good Afternoon,

       

      I am trying to get a form to do a bunch of different calculations. In the included picture, I will be referencing only items under Property A. A client types their "Monthly Income" and "Annual Income" into the appropriate fields, along with "Advertising, Auto and Travel, Cleaning and Maintenance, etc" The field for "Total Annual Expenses" needs to calculate the sum of all the expenses entered (I need a script for that). Then, the (read only) "NET INCOME" field should subtract the (read only)"Total Annual Expenses" from the entered "Annual Income". My issue is that the fields that show calculated values, show "$0.00" when the form is blank. So I used this script to hide them until something has been entered in the "NET INCOME" field:

      var res = +this.getField("AnnualIncome0").value - this.getField("TotalAnnualExpenses0").value;

       

       

      if (res != 0) {

          event.value = res; 

      }

      else {

          event.value = "";

      }

      For some reason though, it is calculating incorrectly. Not sure why as I have never really programmed before.

       

      On top of that, when I enter an "Expense", in stead of getting the sum, it is literally putting the numbers together. For example, if I have $1.00 in the "Auto and Travel" field and $25.00 in the "Cleaning and Maintenance" field, the total should be $26.00, except it shows as $125.00 in the "Total Annual Expenses" field. Here is the script I used:

       

      var res = +this.getField("Advertising.0").value + this.getField("Auto and Travel.0").value + this.getField("Repairs.0").value + this.getField("Cleaning and Maintenance.0").value;

       

       

      if (res != 0) {

          event.value = res; 

      }

      else {

          event.value = "";

      }

       

      I have attached the pictures to show you what I mean. Any help would be greatly appreciated.

       

      Thank you!I+E Problem.JPG

        • 1. Re: Calculation Script Question
          George_Johnson MVP & Adobe Community Professional

          For the second script, it should be:

           

          var res = +this.getField("Advertising.0").value + +this.getField("Auto and Travel.0").value + +this.getField("Repairs.0").value + +this.getField("Cleaning and Maintenance.0").value;


          This will result in numeric addition of the field values instead of the string concatenation you're getting, because the + operator explicitly converts the value to a number.

           

          Be sure to check the field calculation order and set it to whatever makes sense for the form

          • 2. Re: Calculation Script Question
            Maronberg Level 1

            Thank you for your reply. So if a client were to type into a field such "Repairs" and then "Commissions" , that would cause an issue?

            • 3. Re: Calculation Script Question
              George_Johnson MVP & Adobe Community Professional

              I'm not sure I understand your question.

               

              If you fix the script and set the field calculation order correctly, it will work just fine. Note that you should set the calculated fields to read-only so the user can attempt to interact with them.

              • 4. Re: Calculation Script Question
                Maronberg Level 1

                To clarify, values have to be entered into the fields in the order thats written in the script?

                • 5. Re: Calculation Script Question
                  George_Johnson MVP & Adobe Community Professional

                  No, it doesn't matter at all, as long as the script is corrected.

                  • 6. Re: Calculation Script Question
                    Maronberg Level 1

                    Ok great. It is working perfectly. Any idea how to fix the first issue?

                    • 7. Re: Calculation Script Question
                      George_Johnson MVP & Adobe Community Professional

                      In your screen shot the annual income is 100 and the annual expenses is 125, and the result is -25, which seems to be correct. Fixing the second issue should have fixed the first.

                       

                      If not, do you see any errors in the JavaScript console when you press Ctrl+J? Did you confirm that the field calculation order is correct?

                      • 8. Re: Re: Calculation Script Question
                        Maronberg Level 1

                        Ok so I have re-adjusted the code for "Total Annual Expenses" to:

                        var res = +this.getField("Advertising.0").value + +this.getField("Auto and Travel.0").value + +this.getField("Cleaning and Maintenance.0").value + +this.getField("Commissions.0").value + +this.getField("Insurance.0").value + +this.getField("Legal and Professional.0").value + +this.getField("Management Fees.0").value + +this.getField("Repairs.0").value + +this.getField("Supplies.0").value + +this.getField("Real Estate Taxes.0").value + +this.getField("Utilities.0").value + +this.getField("Other.0").value;

                         

                         

                        if (res != 0) {

                            event.value = res;

                        }

                        else {

                            event.value = "";

                        }

                         

                        That field is working perfectly. But when I type everything in, the "NET INCOME" field does not calculate properly. I've added the screenshot so you can see what I am talking about. It is showing the answer to be $90.00 except $100.00-$20.00 is $80 and I have no clue why it isn't working right. PDF Issue.JPG

                         

                        This is the code entered in the "NET INCOME" field:

                         

                        var res = +this.getField("AnnualIncome0").value - +this.getField("TotalAnnualExpenses0").value;

                         

                         

                        if (res != 0) {

                            event.value = res; 

                        }

                        else {

                            event.value = "";

                        }

                        • 9. Re: Re: Calculation Script Question
                          George_Johnson MVP & Adobe Community Professional

                          I'll ask again, because the answer is important: Did you confirm that the field calculation order is correct?

                           

                          In case it's not clear what I mean by this, you set the field calculation order in Acrobat 11 by going into form editing mode (Tools > Forms > Edit) and then selecting: Tasks > Other Tasks > Set Field Calculation Order

                           

                          You have to order the fields according to what makes sense. For example the Net Income field should be last and the Total Annual Expenses should be second to last.

                          • 10. Re: Re: Calculation Script Question
                            Maronberg Level 1

                            I did completely misunderstand you. It works now. Thank you so much! You are the man. Before I lose you, one more quick question I forgot to ask before, what script would be used to multiply "NET INCOME" by 12?