7 Replies Latest reply on Oct 7, 2014 12:40 PM by sparrowce

    Count Checkboxes, Format color depending on value

    sparrowce Level 1

      Have been searching for how to count checkboxes in a PDF form. Found this link, and have renamed my checkboxes so they share the same beginning letters.  Added below script to the custom calculation script section of a number field.  Nothing is happening yet.  What do I need to know to set this up properly?  Also I need to add one more array so it adds another section to the count.  Those checkboxes begin with cbxSAP.  Once the checkbox count is working, is there a way to tell it if sum is = or greater than 14 to color the field/border green, and the default would be red?  Hope this makes sense, thank you.

       

      // Custom calculate script
      
      (function () {
      
      
      
          // Get an array of the PO checkbox fields
      
          var fa = getField("cbxPO").getArray();
      
      
      
          // Initialize counter
      
          var sum = 0;
      
      
      
          // Loop through the fields and update counter
      
          for (var i = 0; i < fa.length; i += 1) {
      
              sum += fa[i].value !== "Off" ? 1 : 0;
      
          }
      
      
      
          // Set this field's value to the sum
      
          event.value = sum;
      
      
      
      })();
      
        • 1. Re: Count Checkboxes, Format color depending on value
          George_Johnson MVP & Adobe Community Professional

          Can you give examples of your actual field names, not just what they begin with?

           

          To answer you last question, yes, but lets get the first part working first.

          • 2. Re: Count Checkboxes, Format color depending on value
            gkaiseril MVP & Adobe Community Professional

            Did you open the JavaScript console to see if there are any errors?

             

            With check boxes have a name structure like "cbxPO.#" The code works for me.

             

            Try:

             

            try {

            // Get an array of the PO checkbox fields 

            var fa = this.getField("cbxPO").getArray(); 

            // Initialize counter;

            var sum = 0; 

            // Loop through the fields and update counter; 

            for (var i = 0; i < fa.length; i += 1) { 

            sum += fa[i].value !== "Off" ? 1 : 0; 

            // Set this field's value to the sum; 

            event.value = sum; 

            } catch(e) {

            console.show();

            console.clear();

            for(var i in e) {

            console.println(i +": " + e[i]);

            // end catch;

            }

            } // end try;


            • 3. Re: Count Checkboxes, Format color depending on value
              sparrowce Level 1

              Field name examples:

               

              cbxPODest

              cbxPOTax

              cbxPOPay

              cbxPOStatus

              cbxPOFreight

              • 4. Re: Count Checkboxes, Format color depending on value
                sparrowce Level 1

                JavaScript error:

                message: this.getField("cbxPO") is null
                fileName: Field:Calculate
                lineNumber: 5
                stack: @Field:Calculate:5

                name: TypeError

                 

                I'm placing it in the custom calculation script of calculate tab on the field where I would like the count to show up.  Am wondering if I've placed it in the right place.

                • 5. Re: Count Checkboxes, Format color depending on value
                  gkaiseril MVP & Adobe Community Professional

                  You did get an error and that is why the JavaScript console opened and displayed those lines of information.

                   

                  The first line states there is an eorror.

                   

                  The code is for a custom calculation script.

                   

                  The code cannot locate the group of fields named "cbxPO". Check to make sure the fields with a name "cbxPO.#" exist. The "#" is the subfield number or name.

                   

                  The code actually works if there are no errors.

                   

                  For hierarchical fields there has to be "." separating the levels. You field names should be:

                   

                  cbxPO.Dest

                  cbxPO.Tax

                  cbxPO.Pay

                  cbxPO.Status

                  cbxPO.Freight

                   

                  And it is not important what the export values used.

                  • 6. Re: Count Checkboxes, Format color depending on value
                    sparrowce Level 1

                    Thank you.  I had just edited my reply as I'd failed to correct it when I added the error code, sorry for that.  I did not have the .# in field names.  Changed them all to have cbxPO.# .... and it works great. Exactly what I was hoping for.  How do I add one more array to count?  There will be 2 counts total.  One if it's a PO, one if Contract.  Now that you've shown me how to count the checked ones, I will apply same to cbxContract.# fields.  However, both types will also have 3 more checkboxes to count - those checboxes apply to both types.  Once the final count is complete, that is where I'm wanting to format the colors.  Thank you so much for your help.

                    • 7. Re: Count Checkboxes, Format color depending on value
                      sparrowce Level 1

                      Update: with this link, was able to sum the arrays.  All is well, am working on the color formatting now.  Really appreciate the help, this will be great, thank you.