6 Replies Latest reply on Nov 1, 2016 5:18 PM by John Frederick Chionglo

    Validation script calculation remains a step behind

    Danelsan2

      Hello. I'm working on a project that has rows of 3 text fields for which the values must be added and as a total for that row in a fourth field. This goes on for many lines. Something like this:

      [total.0] = [A.0] + [B.0] + [C.0]

      [total.1] = [A.1] + [B.1] + [C.1]

      [total.2] = [A.2] + [B.2] + [C.2]

      and so on.

       

      I wanted a script that calculated only the appropriate totals when one of the corresponding values was changed, instead of recalculating everything all the time.

      It was suggested to me that I should use a validation script, so that it would only run when a value was committed to that text field. I know very little of Javascript, but after reading around a bit, I had this set up as a document level script:

       

      function CalcTotals()

      {

      //I believe this gives me the number post "." on the field name, which will correspond to each row that needs to be added up

      var N= event.target.name.split(".").pop();

       

      //The next 3 variables are supposed to give me the values of A, B and C corresponding to the appropriate row

      var Va = +getField("A." + N).value;

      var Vb = +getField("B." + N).value;

      var Vc = +getField("C." + N).value;

       

      //Then I'd add up that row

       

      var T = Va + Vb + Vc;

       

      //And assign it to the corresponding total field

       

      getField("total." + N).value = T;

      }

       

      After this, I tested it out, running CalcTotals(); as a custom validation script in first A, B and C fields.

       

      The issue I'm having is that, while total.o does become the sum of A.0, B.0 and C.0, it does so a step behind. like so:

      starting with everything zero:

      A.0 = 0

      B.0 = 0

      C.0 = 0

      total.0 = 0

       

      Change one value and the total remains zero

      A.0 = 3

      B.0 = 0

      C.0 = 0

      total.0 = 0

       

      Change a second value, and the total becomes the previously correct sum:

      A.0 = 3

      B.0 = 0

      C.0 = 2

      total.0 = 3

       

      A third change would give a total of the result after the second change:

      A.0 = 3

      B.0 = 7

      C.0 = 2

      total.0 = 5

       

      And so on. The calculation is always one changed value behind. Any ideas on how to fix that, while keeping the calculation happening only for the appropriate roll when one the values is changed? Preferably with a document level script that I can just call as appropriate, instead of having to write individual scripts for each roll.

        • 1. Re: Validation script calculation remains a step behind
          Karl Heinz Kremer Adobe Community Professional

          Whenever your validation looks like it is a step behind, you very likely have a problem with your calculation order. Something gets calculated before the necessary information is available. Look at your calculation order and make sure that things that other calculations depend on are calculated first.

          • 2. Re: Validation script calculation remains a step behind
            try67 MVP & Adobe Community Professional

            You can't just plug a calculation script as a validation script... They are different things that access different properties of the event object, for different purposes.

            • 3. Re: Validation script calculation remains a step behind
              gkaiseril MVP & Adobe Community Professional

              As noted the most likely suspect is calculated fields being calculated in the incorrect order. This can be adjusted by reordering the fields to be calculated in the correct order or writing one calculation script for the last field to be calculated that performs all the needed calculations and sets the values of the calculated fields.

               

              Note the calculation order for fields is different from the tab order for field entry and the reading order for a PDF.

              • 4. Re: Validation script calculation remains a step behind
                Danelsan2 Level 1

                Thanks for the answers so far.

                 

                So, let me take this back from the start. Is there a way to make it so the Form doesn't keep recalculating every calculation all the time? Calculate only the stuff that is affect by a field that gets a new value? That is what I'm trying to do.

                 

                Otherwise just setting calling my script as Calculation for each Total field works fine. I was trying to figure out if there is a way to change, say, [B.20], and having it recalculate only [Total.20], instead of going through all the Totals.

                 

                Unless my understanding is wrong and it does recalculate only what is relevant by default?

                • 5. Re: Validation script calculation remains a step behind
                  Karl Heinz Kremer Adobe Community Professional

                  No, you cannot do selective calculations. When the state of your form changes, all fields with calculation scripts will be re-calculated.

                   

                  The only way you can do this differently is by having only one calculation script, implemented as a document level script. Then you call this script from one field. In this script, you can then determine which field was changed (using the event.source property), and the tailor your calculation to that. I don't bother with that, I just use one calculation script to get around any problems with the calculation order.

                  • 6. Re: Validation script calculation remains a step behind
                    John Frederick Chionglo Level 1

                    Assume the names of four text fields: “total.0”, “A.0”, “B.0”, and “C.0”. The value of “total.0” is the sum total of the values of
                    “A.0”, “B.0” and “C.0”.

                     

                    Assume each of “A.0”, “B.0” and “C.0” has a validation script.

                     

                    Assume the following script defined at the document level:

                     

                    var t0 = this.getField(“total.0”);

                    var a0 = this.getField(“A.0”);

                    var b0 = this.getField(“B.0”);

                    var c0 = this.getField(“C.0”);

                    t0.value=3;

                    a0.value=3;

                    b0.value=0;

                    c0.value =0;

                     

                    Assume the user has entered 5 in field “A.0”.

                     

                    When the validation script runs for “A.0” and if the validation script uses event.value and a0.value, then the script will
                    find that event.value is 5 while a0.value is still 3.

                     

                    Another way to compute the value of “total.0” when the value of “A.0” changes would be to perform the following computation in the
                    validation script of “A.0”:

                     

                    t0.value = t0.value + (event.value – a0.value);

                     

                    A similar computation is performed in the validation script of “B.0”:

                     

                    t0.value = t0.value + (event.value – b0.value);

                     

                    And a similar computation is performed in the validation script of “C.0”:

                     

                    t0.value = t0.value + (event.value – c0.value);