7 Replies Latest reply on Jul 21, 2011 12:18 PM by campy3

    User changing values of performed calculation

    campy3

      Is there a way in acrobat for the user to change the value of a calculated field once a calculation has already been performed?

       

      Is there a script you can create to over-ride the calculation being performed?

       

      Thanks

        • 1. Re: User changing values of performed calculation
          George_Johnson MVP & Adobe Community Professional

          Sure there is. In the script, look at the value of event.source, and if it's the same as event.target, don't perform the calclation. This allows the user to enter a different value than what would normally get calculated.

           

          Note that the value will get recalculated the next time another field value changes, replacing the overridden value, unless you add additional code to prevent it. If you need more help, post again with what code you've come up with and we can make suggestions.

          1 person found this helpful
          • 2. Re: User changing values of performed calculation
            campy3 Level 1

            var Field_Name = 'tax1';

             

            var taxrate1 = 4.5;

             

            if (Number(this.getField(Field_Name).value.length) != 0) {

             

            taxrate1 = Number(this.getField(Field_Name).value);

             

            event.value = taxrate1 * this.getField('taxbase').value;

             

             

             

            Taxrate often changes so I created an option field that changes the end result if any value is entered. Rather than getting too involved in rounding formulas I would like the option for the user to round or change calculated values as they wish. This is outside the normal definitely for what adobe intended, so I wasn’t sure if this was an option.  I kinda understand what you are saying, but not exactly sure how to code it.

             

            Thanks again very much for any help.

            • 3. Re: User changing values of performed calculation
              George_Johnson MVP & Adobe Community Professional

              Here is how I would be inclined to code the calculation script:

               

              // Custom Calculate script for text field
              (function () {
              
                  // Set up some variables
                  var val1, taxrate1 = 4.5;
              
                  // Get a reference to the tax rate field and the taxbase field
                  var f1 = getField("tax1");
                  var f2 = getField("taxbase");
              
                  // Only perform the calculation if triggered by this field the input fields
                  if (event.source && (event.source === f1 || event.source === f2)) {
              
                      // Get the tax rate field value, as a string
                      val1 = f1.valueAsString;
              
                      // If the user entered a different tax rate, convert to a number and use it
                      if (val1) taxrate1 = +val1;
              
                      // Perform the calculation
                      event.value = taxrate1 * f2.value;
                  }
              
              })();
              

               

              The user will be able to enter a different value in this field if they want, but it will get recallculated if the tax rate or taxbase field value subsequently changes.

              1 person found this helpful
              • 4. Re: User changing values of performed calculation
                campy3 Level 1

                George,

                 

                That is really neet.  You came up with an interesting way to solve this problem.

                 

                I am still slightly missing one piece of the puzzle, although i think its the way i explained it. 

                 

                 

                So previously I had a field, tax1 that is empty.  The text basically went like this:

                 

                tax interest rate 4.5 or _____ (tax1 field)

                 

                Using the old coding it was set up like so to handle this.

                var Field_Name = 'tax1';
                // default rate

                var taxrate1 = 4.5;

                if (Number(this.getField(Field_Name).value.length) != 0) {

                taxrate1 = Number(this.getField(Field_Name).value);

                event.value = taxrate1 * this.getField('taxbase').value;

                 

                So before with the old coding it would calculate using 4.5 if nothing was entered in the tax1 field.  If the value is zero nothing is calculated, and anything but zero will calculate using whatever the user enters.  With this coding you provided it will calculate everything once a value is entered in the tax1 field, but I would like it to calculate automatically unless the value is 0 (or left blank).  Any idea how to adjust?

                 

                Thanks again

                • 5. Re: User changing values of performed calculation
                  George_Johnson MVP & Adobe Community Professional

                  What I posted will automatically calculate the field whenever either the tax1 or taxbase field value changes. If the tax1 field is blank, it will use the default value of 4.5. The user is then able to manually change the value of the calculated field if desired.

                   

                  For what it's worth, the following line of code probably does not do what you think it does.

                   

                  (Number(this.getField(Field_Name).value.length) != 0)
                  

                   

                  When the value of the field is numeric (e.g., 1.2), the data type of the value will be number, and since a number does not have a length property, it will return the special value of "undefined". When undefined is converted to a number using Number, it returns the special numeric value of NaN. Since NaN is not equal to zero, the result of the comparison in the if statement will always be false. That's why I retrieved the value of the field as a string, since it is easy to see if the field is blank and you can simply use the + operator to convert it to a number.

                   

                  It doesn't appear as though you posted the entire code that you were using (note the missing closing curly bracket), so I'm not sure the revised code is doing what you want. Is the following true:

                   

                  1. When the tax rate field is blank, calculate using the default value of 4.5

                  2. When the tax rate field has a value that's not equal to 0, calculate using the value in the tax rate field.

                   

                  Exactly what should happen if the tax rate field is not blank but the value is equivalent to zero (e.g., "0", "0.00", ".", "-", etc.)?

                   

                  Do you want the user to be able to enter whatever they want in the calculated field, thus manually overriding the calculated value?

                  • 6. Re: User changing values of performed calculation
                    campy3 Level 1

                    I see what you mean now.  Makes since, the original code was just kind of a beginners attempt using work-arounds.  You got any recommendations on books for learning java?

                     

                    It wil calculate if I enter a value in tax1, but it will not calculate when tax1 is empty.  And I can manually enter a value in the calculated field, so that works too which is really nice.

                    • 7. Re: User changing values of performed calculation
                      campy3 Level 1

                      Just one thing to add.  If i enter a value into tax1 it works flawlessly.  If i change that value to zero it performs the calculation as intended using the default of 4.5.  It just won't do it automatically until I change the value of tax1.