8 Replies Latest reply on Dec 16, 2017 2:30 PM by campbellc36855280

    Populating Prices from Dropdown Box

    LordJay

      Hi,

      I have been trying to write a script that automatically populates a price in another field once an item in a dropdown box is selected. From looking at the tutorials, I have a good portion completed but I am running into the issue where every "cost" field is populated and I am not sure how to create a 1:1 selection with each row.

       

      Example: desc1 populates for cost1

                      desc2 populates for cost2

       

       

      Code:

      var cateringData = { "Large Cheese Board":{ cost: "165"},

                                       "Regular Cheese Board":{cost: "98"}};

       

      function setFieldValues(cCateringName)

      {

        this.getField("cost1").value = cateringData [cCateringName].cost;

        this.getField("cost2").value = cateringData [cCateringName].cost;

        this.getField("cost3").value = cateringData [cCateringName].cost;

        this.getField("cost4").value = cateringData [cCateringName].cost;

        this.getField("cost5").value = cateringData [cCateringName].cost;

        this.getField("cost6").value = cateringData [cCateringName].cost;

        this.getField("cost7").value = cateringData [cCateringName].cost;

        this.getField("cost8").value = cateringData [cCateringName].cost;

        this.getField("cost9").value = cateringData [cCateringName].cost;

        this.getField("cost10").value = cateringData [cCateringName].cost; }

       

      Then this code is in the dropdown box field (desc1),

      if( event.willCommit ) {

          if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value); }

       

      Screen Shot 2017-11-30 at 5.03.33 PM.png

       

      Any help is much appreciated!

       

      Thanks!

        • 1. Re: Populating Prices from Dropdown Box
          Thom Parker Adobe Community Professional

          So you have one dropdown for each cost field? Then all you need is a simple way to specify the associated cost field, when a particular dropdown is selected.

           

          Modify your script so the name of the associated field is passed into the function that modifies the field value

          function setFieldValues(cCateringName, cCostFieldName)

          {

            this.getField(cCostFieldName).value = cateringData [cCateringName].cost;

          }

           

          The dropdown code for the desc1 field includes the name of the field it will affect:

          if( event.willCommit ) {

              if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value,"cost1"); }

           

           

          I personally prefer self-aligning systems, where the target fields are derived from the name of the current field. For this you'd add code to the Dropdown keystroke code:

           

          var cCostName = event.targetName.replace(/desc/,"cost");

           

          This code automatically generates the name of the associated field.

          • 2. Re: Populating Prices from Dropdown Box
            LordJay Level 1

            Awesome! Thank you so much for helping me.

            • 3. Re: Populating Prices from Dropdown Box
              campbellc36855280

              Hi,

               

              I ran into an issue where if I try to add custom text to the dropdown fields, I get this error.

               

              setFieldValues, script Document-Level:setField Values

              Exception in line 2 of function top_level, script AcroForm:desc2:Keystroke

              TypeError: cateringData[cCateringName] is undefined

              74:Document-Level:setField Values

               

              I’ve been trying to write a regular expression in the cateringData object as a “catchall” but I can’t get it working. Is their an easier way to accomplish this?

               

              Thanks Again

              • 4. Re: Populating Prices from Dropdown Box
                Thom Parker Adobe Community Professional

                The error is caused by an undefined value. Have you checked this? Do you know why it is undefined?

                 

                What do you mean by a regular expression as a catchall? How is this RegEx used?

                • 5. Re: Populating Prices from Dropdown Box
                  campbellc36855280 Level 1

                  Hi,

                   

                  It is undefined because it is not one of the items in cateringData. I am trying to set it up, so if the item is not in the list we can still add it manually.

                   

                  I was trying to use the RegEx like this,

                   

                  var cateringData = { "Add Gluten Free":{ cost: "2"},

                                       "Bacon,Egg,& Cheese Biscuit":{ cost: "8"},

                                       "Banana Bread Loaf, 8 slices":{ cost: "28"},

                                       "/*/": { cost: "/*/"}};

                   

                  Thanks

                  • 6. Re: Populating Prices from Dropdown Box
                    try67 MVP & Adobe Community Professional

                    You can't use regular expressions with this kind of literal object. You

                    would need a different approach to be able to do that.

                     

                    On 12 December 2017 at 22:26, campbellc36855280 <forums_noreply@adobe.com>

                    • 7. Re: Populating Prices from Dropdown Box
                      Thom Parker Adobe Community Professional

                      So as try states, you can't use the regEx in this way.  But you can catch the error and respond to it.

                      To do this you'll need to break up the code that uses this list into two parts: 1) acquire object from catering data, 2) check for a valid value before using it.

                       

                      So here is a redo:

                       

                      function setFieldValues(cCateringName, cCostFieldName)

                      {

                        var oItemData = cateringData [cCateringName];

                        if(oItemData)

                            this.getField(cCostFieldName).value = oItemData.cost;

                        else

                        { ... do something else ... }

                      }