5 Replies Latest reply on Apr 27, 2010 12:47 PM by Angetes

    Joining field values into one field

    Angetes

      Hi

      I have 4 fields on a form

      a text field
      a combo box holding text values
      a date field

      the 4th field has a custom calculation script which holds some extra text combined with the values of the other 3 fields.


      What I would like is the 4th field to calculate only when all three fields are filled and if they aren't it be left blank.

       

      I have the code below as the custom calculation script.

       

      var d = this.getField("cname").value.toString() 
      var e = this.getField("cbox").value.toString() 
      var f = util.scand("dd/mm/yy",this.getField("cdate2").value)

       

      if( (d.length > 0) && (e.length > 1) && (f.length > 0))
      {
      this.getField("joined").value = "Some text" "+ d +" "+ e +" some text " + util.printd("dd mmmm yyyy",f)
      }
      else
      {
      this.getField("joined").value = ""
      }

       

      Unfortunately it doesn't work though I've no idea why.

       

      Any pointers would be greatly appreciated

       

      TIA

        • 1. Re: Joining field values into one field
          try67 MVP & Adobe Community Professional

          Are there any errors? What does happen?

           

          Also, if this script is the custom calculation script for the field

          "joined", use

          event.value =

          instead of

          this.getField("joined").value =

          • 2. Re: Joining field values into one field
            gkaiseril MVP & Adobe Community Professional

            Have you looked at the Acrobat JS API Reference and the Mozilla JS Core 1.5 Reference documents?

             

            The 'util.sand()' returns a date object and not a value. The result has no value, no length. You need to use speicif methods to extract data from this object. I think you want to use just the value of the date field. Combo boxes can not have any null value.

             

            You could also use the Acrobat JS console object or the 'app.alert()' method to display information about what you are processing and the progress of your script.

             

            console.show(); // show the console
            console.clear(); // clear the console


            // get the name field
            var sName = this.getField("cname").valueAsString
            // some properties of value
            console.println('cname:');
            console.println('typeof: ' + typeof sName);
            console.println('value: ' + sName);
            console.println('length: ' + sName.length);

            var sChoice = this.getField("cbox").valueAsString; 
            // some properties of value
            console.println('cbox:');
            console.println('typeof: ' + typeof sChoice);
            console.println('value: ' + sChoice);
            console.println('length: ' + sChoice.length);
            // fix for the combo box's default value of blank to a null
            if(sChoice == ' ') sChoice = '';

            var sDate = this.getField("cdate2").valueAsString;
            // some properties of value
            console.println('cdate2:');
            console.println('typeof: ' + typeof sDate);
            console.println('value: ' + sDate);
            console.println('length: ' + sDate.length);
            // reformat date string
            var oDate = util.scand("dd/mm/yy", sDate); // create date object
            var sNewDate =  util.printd("dd mmmm yyyy",oDate);
            console.println('reformatted: ' + sNewDate);

            console.println('sName not null: ' + (sName != '') )
            console.println('sChoice not null: ' + (sChoice != '') )
            console.println('sDate not null: ' + (sDate != '') );

            if( sName != '' & sChoice != '' & sDate != '') {
            event.value = "Some text " + sName + " " + sChoice  + " some text " + sNewDate;
            }

            • 3. Re: Joining field values into one field
              Angetes Level 1

              Hi

               

              Thanks for the reply.

               

              There are no error messages it just doesn't work (lol)

               

              I've amended the code to this:

               

              var d = this.getField("cname").value.toString() 
              var e = this.getField("cbox").value.toString() 
              var f = util.scand("dd/mm/yy",this.getField("cdate2").value)
              var g = util.printd("dd mmmm yyyy",f)

              if( (d.length > 0) && (e.length > 1) && (g.length > 0))
              {
              event.value = "Some text " + d +" "+ e +" some text " + g
              }
              else
              {
              event.value = ""
              }

              I figured possibly the date needed a variable to be parsed back to text but it still didn't work.

              • 4. Re: Joining field values into one field
                try67 MVP & Adobe Community Professional

                Start debugging.

                Print d, e, and g to the console and make sure they are correct.

                • 5. Re: Joining field values into one field
                  Angetes Level 1

                  Hi

                   

                  Thanks for the reply.

                   

                  I'm not a programmer, more like an enthusiastic 'one step up from the bottom' beginner who is always looking to improve my skills.

                   

                  I did manage to figure out how to achieve my goal from your input with the following code

                   

                  var d = this.getField("cname").valueAsString 
                  var e = this.getField("cbox").valueAsString 
                  var f = this.getField("cdate2").valueAsString
                  var g = util.scand("dd/mm/yy", f)
                  var h = util.printd("dd mmmm yyyy",g)

                  if( d != '' & e != ' ' & f != '')

                  {
                  event.value = "Some text " + d + " " + e  + " some text " + h;
                  }

                  else
                  {
                  event.value = ""
                  }

                  the minor change I made was to insert a single space value into the combo box to have as the default value for the field.

                   

                  It all works beautifully!

                   

                  Thanks again.