1 Reply Latest reply on Mar 8, 2012 1:51 PM by George_Johnson

    How to make a script that only runs if another field is a certain value

    chrisjones1120

      I have a sales tax field on my form,

       

      I only charge sales tax In TX,  So when they choose the state TX on that field. I want Sales Tax to be implemented. 


      I am trying to search on how exactly to do this and am not having any luck.

        • 1. Re: How to make a script that only runs if another field is a certain value
          George_Johnson MVP & Adobe Community Professional

          If this is for a calculated field, you can use a custom JavaScript for this, something along the lines of:

           

           

          // Calculate script for text field
          (function () {
          
              // Get the value of the State field
              var state = getField("State").value;
          
              // Get the subtotal
              vat total = getField("SubTotal").value;
          
              // Set the tax rate
              var taxRateTX = 0.0825  // 8.25%
          
              // Calculate the tax if Texas
              if (state === "TX") {
                  event.value = util.printf("%.2f", total * taxRateTX);
              } else {
                  event.value = "";
              }
          
          })();
          

           

          Change the field names in the code to match your form. I'm assuming the the state is selected from a combo box (dropdown) and that it gives a value of "TX" when Texas is selected. This code rounds to the nearest cent (assuming SubTotal is in dollars) which may not be correct.