2 Replies Latest reply on May 27, 2015 1:49 PM by ans9090

    Help with writing javascript formula

    ans9090

      I am new at javascript and have been asked to write one for a field on an adobe form.

       

       

      In the form there is a script written to calculate the value that goes into field A. I need a script written that checks the value calculated in A to see if it is greater than 50% of the value in field B and if there is anything populated in field C. If those two guidelines are met, I need the number presented in field A to be capped at the 50% value.

       

      Can someone help me in writing this?

       

      Thank you!

        • 1. Re: Help with writing javascript formula
          gkaiseril MVP & Adobe Community Professional

          I would use the custom JavaScript for calculating the value of field A. Using JavaScript, one can get the value of any field and then use the "if else" statement to control the execution of blocks of code within the calculation script.

           

          // Custom calculation for field A;

          event.value = "";

          var B = this.getField("B").valueAsString; // get the value of field B.

          var C = this.getField("C").valueAsString; // get the value of field C;

          // compute only if fields B and C are not empty strings;

          if(B  != "" && C != "") {

          // limit value of A to inputted value or 0.5 of B, which ever is smaller;

          event.value = Math.min(event.value, Number(B) * 0,5);

          }

          • 2. Re: Help with writing javascript formula
            ans9090 Level 1

            Thank you for the help. I did not know there was already a script in there and I am not sure how to get the two to work together. Here is what is currently in there:

             

             

            var A = this.getField("ExpenseType").value

            var B  = this.getField("PurchaseType").value

            var C  = this.getField("NegFinalPrice").value

            var D  = this.getField("CurrentVolume").value

            var E  = this.getField("BudgetifExists").value;

            if ((A == "CAPEX") && (B == "New") && (C != "") && (D != "") && (E != "") || (A == "CAPEX") && (B == "Recurring") && (C != "") && (D != "") && (E != ""))

            {

                 event.value = (E-C)*D;

            } else {event.value = "";}

             

             

             

            When I was referring to fields A,B, and C, this is what they are labeled as on the form...field A: ECCORecurring, field B: POorContractAmount, and field C: FSSBaselineSource. Could you help getting the first script to run then the second base off of the value calculated by that one?