3 Replies Latest reply on Jun 21, 2011 7:16 PM by George_Johnson

    Adobe If/then statement problem

    DRKLotus913

      I am making a pdf with a few if then statements which will affect multiple fields. For example I have a a dropdown field called Armor with multiple values. So if for example I select Breastplate I want a value of -4 to appear in a seperate field called CheckPen.

       

      I have tried the following javascript on both Armor and CheckPen but it didn't work:

       

      if(Armor="Breastplate")then(CheckPen=-4);

       

       

      I also tried:

       

      if(this.getField("Armor")="Breastplate")then(this.getField("CheckPen")=-4);

       

      and finally I tried this as a javascript in the CheckPen field:

       

      var Armor=this.getField("Armor").value;

      if(Armor="Breastplate")event.value=-4;

       

      Nothing seems to work can anyone help?

        • 1. Re: Adobe If/then statement problem
          George_Johnson MVP & Adobe Community Professional

          Are you using Acrobat or LiveCycle Designer to create this form?

          • 2. Re: Adobe If/then statement problem
            DRKLotus913 Level 1

            Adobe Acrobat X Pro

            • 3. Re: Adobe If/then statement problem
              George_Johnson MVP & Adobe Community Professional

              So you're using a combo box. I'd suggest setting the export value of each item to the value associated with the item. So in your example, the Breastplate item would have an export value of -4. But only do this if the values associated with the items are unique. That is, the Helmet item or anything else will not have a value of -4.

               

              Assuming the CheckPen field is text box that the user does not need to interact with, you should set it to read-only. You can then use something like the following the the custom Calculate script for the CheckPen field:

               

              (function () {
              
                  // Get the value of the Armor field
                  var v = getField("Armor").value;
              
                  // Set this field value to the export value of the selected item
                  event.value = v;
              
              })();
              

               

              The first and last lines prevent the unnecessary creation of global variables, which is good practice.

               

              The other option is to set the value of the CheckPen field with a script in the Keystroke event of the combo box. Something like:

               

              // Custom Keystroke script for combo box
              
              // Set the value of the text field to the export value of the selected item
              if (!event.willCommit) {
                  getField("CheckPen").value = event.changeEx;
              }
              

               

              This code assumes you have set the "Commit selected value immediately" option for the combo box. This is what I prefer to use.

               

              If it tunrs out that the export values of each of the combo box items is not unique, it gets a bit more complicated, but you can set up a simple lookup table to associate a unique export value with the point value for the item. For example, suppose you have ten items and give then export values of 0, 1, 2, 3...9. You can then set up an array to associate the export value with the point value:

               

              // Custom Keystroke script for combo box
              
              // Setup the lookup table array
              var arr = [0, 1, 2, -4, -4, -2, -3, 1, -3, 2];
              
              // Set the value of the text field to the assoociated value of the selected item
              if (!event.willCommit) {
                  getField("CheckPen").value = arr[event.changeEx];
              }
              

               

              As you can see, the array (arr) has several elements that are the same as others.

               

              If you will be doing a lot of JavaScript programming, you should get a decent language reference that you can consult. The code that you posted has a number of syntax problems.