4 Replies Latest reply on Mar 5, 2009 3:37 PM by (J_Hinckley)

    Something is wrong with my subTotal script

      The script actually works fine until you skip a line in the the form. Then the amount from that line is appended to the end of the Subtotal. For example: If the Price.0 field is $100.00 and the Price.1 field is $100.00, the Subtotal field shows $200.00. So far so good. Now if Price.3 is skipped and $50.00 is entered in Price.4, the Subtotal field shows $20,050.00.

      Here's the script:

      var subTotal = this.getField("Price.0").value
      + this.getField("Price.1").value
      + this.getField("Price.2").value
      + this.getField("Price.3").value
      + this.getField("Price.4").value
      + this.getField("Price.5").value
      + this.getField("Price.6").value
      event.value = subTotal;
        • 1. Re: Something is wrong with my subTotal script
          MarkWalsh Level 4
          Why use a script? You can just set your subtotal field to calculate field 'Price"
          • 2. Re: Something is wrong with my subTotal script
            gkaiseril MVP & Adobe Community Professional
            The "+" operator is used for an arithmetic addition or string concatenation and JavaScript looks at the 2 values involved and assumes what operation is being asked for. It the values are 2 numbers addition is assumed, if 2 character strings then concatenation is assumed, and if one value is a number and the other a character string concatenation is assumed. You need to use the "Number()" constrictor to force the calculation to use an arithmetic addition.

            Your script can be simplified by using some document level function to sum an array of fields.

            // document level function
            function SumArray(aValues) {
            var sum = 0; // sum
            // loop through array of values
            for(i = 0; i < aValues.length; i++) {
            sum += Number(aValues[i]);
            } // add element i to sum
            return sum; // return sum
            } // end function

            function MakeAValues(sFieldName) {
            var oField = this.getField(sFieldName); // get field object
            var aFields = oField.getArray(); // convert field object to an array of fields
            var aValues = new Array(aFields.length); // array for the values of the fields
            for(j = 0; j < aFields.length; j++) {
            aValues[j] = aFields[j].value;
            } // end for loop
            return aValues; // pass back array of values
            }
            // end of document level functions

            // custom calculation script to sum a hierarchical group of fields
            // use defined function to:
            // create an array of values
            // sum that array of values
            event.value = SumArray( MakeAValues("Price") );

            You could also use this undocumented funcition:

            AFSimple_Calculate("SUM", "Price");

            But it will hide the script.
            • 3. Re: Something is wrong with my subTotal script
              gkaiseril MVP & Adobe Community Professional
              Also if you research the code behind the aforms.js functions supplied by Adobe, you will see it is basically what I have done. Also by writing a custom script, I could easily add the calculations to perform shipping, sales tax, volume discount, and grand totals.
              • 4. Re: Something is wrong with my subTotal script
                Level 1
                Geo, I entered your script in the JavaScript Editor under Advanced>Document Processing>Document JavaScripts, and when I press OK, I get "missing } after function body 12: line 13. I don't see where the "}" is missing.

                Also, I believe the following lines of code you provided are entered in the Text Field Properties box under the Calculate tab. Please confirm.

                // custom calculation script to sum a hierarchical group of fields
                // use defined function to:
                // create an array of values
                // sum that array of values
                event.value = SumArray( MakeAValues("Price") );

                I've been thinking about the sales tax problem. What I want to do (and maybe there's a better way) is use a Check Box to activate the calculation of the sales tax. I don't want the sales tax calculated automatically because it is only added if the purchaser is from the same state where the business is located. In this case, the rate is 6 percent. It is calculated on the goods only, so the amount in the "Subtotal" field would be used for the calculation.

                Lastly, and to tie it all together, how would I use your script to perform shipping, sales tax and grand totals? You helped me in a previous thread with my shipping problem.

                Thanks for all your help. I'm new to JS and learn best by seeing examples.