8 Replies Latest reply on Oct 28, 2011 10:06 AM by DC Dowd

    How to count ONLY fields with a specific value

    DC Dowd

      I modified the code below from the API reference to count combobox fields with a value of "MT" so I could total the number of a specific choice the form user input.

       

      var count = 0;

      for (var i=0; i<this.numFields; i++)

      {

      var fname = this.getNthFieldName(i);

      if ( this.getField(fname).type == "combobox" && this.getField(fname).value == "MT" ) count++;

      }

      this.getField("mTurb").value = count.value;

       

       

      Here's the problem: I tried to use a similar script, modified only slightly from this form, in another total field to tally a different choice made with comboboxes and the script continues to tally the first choice. An example of one of the modified versions of the script is below.

       

      var gcount = 0;

      for (var i=0; i<this.numFields; i++)

      {

      var gname = this.getNthFieldName(i);

      if ( this.getField(gname).type == "combobox" && this.getField(gname).value == "MT" ) gcount++;

      }

      this.getField("gPack").value = gcount.value;

       

      I suspect I'm just missing something painfully obvious to seasoned scripters, but I can't seem to figure it out and I need this script to be able to count the number of 9 different options in the comboboxes.

        • 1. Re: How to count ONLY fields with a specific value
          George_Johnson MVP & Adobe Community Professional

          If I understand what you're saying, you need to change "MT" in the line below to theother value you want to count.

           

          if ( this.getField(gname).type == "combobox" && this.getField(gname).value == "MT" ) gcount++;

          1 person found this helpful
          • 2. Re: How to count ONLY fields with a specific value
            DC Dowd Level 1

            Changed that and am getting "undefined" in the text fields with this script as their calculate script.

             

            There was a validate script running that created an array, but I (sadly) turned off validation and lost the script. I will check my notes and see if I have the script laying about somewhere.

            • 3. Re: How to count ONLY fields with a specific value
              George_Johnson MVP & Adobe Community Professional

              You should provide more information about how the form is set up and explain exactly what it is you're trying to accomplish. The original script you have is not correct for a custom calculation script and can likely be made more efficient, but you need to provide more details.

              • 4. Re: How to count ONLY fields with a specific value
                DC Dowd Level 1

                The form is for calculating the number of certain units being warranteed. I have 24 comboboxes with the various warrantable units included. The export values are: MT, FGB, GP, DMSC, APS, CPS, RP, CLC, and FOK. The text fields are meant to total how many of each unit have been selected in all 24 comboboxes.

                 

                So, if a user selected MT, MT, GP, DMSC, APS, MT, FGB, MT (in no particular order) then the text fields would tally 4 MT (in its own field), 1 GP (ibid), etc.

                 

                Using Acrobat 9 for making these forms.

                 

                Any and all help is much appreciated.

                • 5. Re: How to count ONLY fields with a specific value
                  George_Johnson MVP & Adobe Community Professional

                  OK, that's good. Just one more thing, what are the names of the combo boxes? The code can be simplified if you used names like combo1, combo2, ...combo24, since you can set up a loop that loops through just the combo box fields and not every field in the form.

                  • 6. Re: How to count ONLY fields with a specific value
                    DC Dowd Level 1

                    The combo boxes are named sequentially, M01 - M24.

                    • 7. Re: How to count ONLY fields with a specific value
                      George_Johnson MVP & Adobe Community Professional

                      OK, you can use a script like the following as the custom calculate script for the text fields that perform the counts:

                       

                       

                      // Custom calculate script for text field
                      (function () {
                      
                          // Declare and initialize variables
                          var fn, s = "MT", count = 0;
                      
                          // Loop through the combo boxes
                          for (var i = 1; i < 25; i++) {
                      
                              // Determine the current field name
                              fn = "M" + util.printf("%02d", i));
                          
                              // See if the field value matches 
                              if (getField(fn).valueAsString === s) count++
                      
                          }
                      
                          // Set this field value to the count
                          event.value = count;
                      
                      })();
                      

                       

                      Change the value of the "s" variable to match the item that you want to count. The first and last lines prevent the unnecessary creatio of global variables, which is good. It also makes it easy to transfer to a more general document-level routine, which would be event better, something like:

                       

                       

                      // Custom calculate script for text field
                      function getCBCount(s) {
                      
                          var fn, count = 0;
                      
                          // Loop through the combo boxes
                          for (var i = 1; i < 25; i++) {
                      
                              // Determine the current field name
                              fn = "M" + util.printf("%02d", i));
                          
                              // See if the field value matches 
                              if (getField(fn).valueAsString === s) count++
                      
                          }
                      
                          // Set this field value to the count
                          event.value = count;
                      
                      }
                      

                       

                      You would then call this function from the individual text boxes with the following custom calculate script:

                       

                      getCBCount("MT");

                       

                      and replace "MT" with the vlaue you want to count.

                      1 person found this helpful
                      • 8. Re: How to count ONLY fields with a specific value
                        DC Dowd Level 1

                        Kept searching the forums for something and ran across a code that I modified as shown. I also changed the naming of the combo boxes (M1 - M24) to allow for easier scripting.

                        var counter = 0;

                        for (i = 1; i <= 24; i++) {

                        if (this.getField("M" + i).value=="MT") counter++;

                        }

                        if (counter>0) {

                        event.value = counter;

                        } else {

                        event.value = "";}

                        I think it was an answer try67 had posted to another question, but it seems to be working perfectly.

                         

                        Thank you for the help. It totally pointed out errors the fixing of which helped get this done.

                         

                        On to the next bits of scripting. This form is killing me.