2 Replies Latest reply on May 24, 2011 8:08 AM by govtech77

    Need value of field to be calculated as 1 or 0

    govtech77

      I have a form that has a column labled Plot number (i.e. 1, 2, 3 etc.) at the bottom of that field I need the total number of plots calculated. I don't want it to add 1+ 2+ 3. I want each to be added as one is there is any text in the box and as 0 if there is no text.

      I tried to write an if else statement, but I am so new to javascript it did not work. Also, I am not sure if I should put the script in each plot number field or in the total field. Here is the script I have in the total number field.

      var nPL1 = this.getField("1").value;
      var nPL2 = this.getField("2").value;
      var nPL3 = this.getField("3").value;
      var nPL4 = this.getField("4").value;
      var nPL5 = this.getField("5").value;
      var nPL6 = this.getField("6").value;
      event.value = this.getField("1").value
      event.value = nPL1+nPL2+nPL3+nPL4+nPL5+nPL6;

       

      This is the if else statement I wrote

      event.value = this.getField("1").value
      if (event.value == null) {
          event.value = 0;
      } else {event.value = 1};

       

      Any help is greatly appreciated

        • 1. Re: Need value of field to be calculated as 1 or 0
          George_Johnson MVP & Adobe Community Professional

          In the field that is to contain the sum of the fields that are not blank, use the following custom Calulcate script:

           

           

           

          // Custom Calculate script for text field
          (function () {
          
              // Initialize the count
              var count = 0;
          
              // Loop through the column fields to determine the count of non-blank fields
              for (var i = 1; i < 7; i++) {
                  if (getField(i).valueAsString !== "") count += 1;
              }
          
              // Set the value of this field to the count
              event.value = count;
          
          })();
          

           

          The first and last lines are there to prevent the unnecessary creation of global variables.

           

          Also, set any calculated fields to read-only so the user does not attempt to interact with it.

           

          Edit: corrected typo

          • 2. Re: Need value of field to be calculated as 1 or 0
            govtech77 Level 1

            Thank you so much. It works perfectly.