8 Replies Latest reply on May 9, 2013 1:44 PM by gkaiseril

    Creating custom calculation script

    glvmlss01

      I have created a PDF ( i'm using Acrobat X Standard) that we will be using for our merit increases.  I have created a Pay1 field (this is for the old rate) Pay2 field (is the new rate) Pay 3 is the difference. I enetered a formula (in the simplified field notation) Pay2-Pay1 to get the difference for Pay3.  This works perfect.  Now I need to get this to reflect the % of increase on another field and I am having trouble getting this to even work.  I entered a simplified notation of (pay2-pay1)/pay3 and it seemed to work but then I was getting error messages.  I am not sure what I did wrong but in doing some research it looks like a custom calc script is the way to go, only I have no idea how to do it.

        • 1. Re: Creating custom calculation script
          gkaiseril MVP & Adobe Community Professional

          What is the exact message?

           

          What conditions cause this message?

           

          What happens if Pay1 and Pay2 fields have a value?

           

          If a field is empty or null what is the numeric value of the field?

           

          What is the result of division by zero?

          • 2. Re: Creating custom calculation script
            glvmlss01 Level 1

            I started getting this message after I entered in the simplified notation and saved it.  I begin trying to fill out the form  (with other employee information). The error message I received is "the value entered does not match the format of field (pay4)".    I would just select the okay button and continue.   I got down to the pay1,2 and would enter an amount and i would get the difference in the pay 3 box, so all that still seems to work fine.

            Even though I had information for pay1,2 and 3 nothing would show up for the precentage field (pay 4).

            If the field is empty everything still shows as zero.

            • 3. Re: Creating custom calculation script
              MrGawain

              First you should set the Default values in Pay1 and Pay2 = 0.  That way your cal wont try to add "" or Not a Number to your equation. 

              Second under format select numbers in the drop down.  One you set the format to number the user should only be able to enter numbers

                  whats more the PDf wont confuse the numbers for strings.  IE it can assume 123 = letters.

              You can then click on Validate tab and limit the rage of number aloud to be entered into Pay1 & Pay2 if you so choose.

               

              All you may need to do is to adjust the setting above but should you need a Java Example continue reading.

               

              A simple custom script could read;  Note a line with // means ignore this line in JavaScript - IE notes.

               

              var P1 = this.getField("Pay1").value;

              var P2 = this.getField("Pay2").value;

              //  Now what ever was entered into Pay1 is represented by "P1"

              //  If you followed the instructions above Pay1 & Pay2 at least = 0

              var D1F = P1-P2;

              this.getField("Pay3").value = D1F;

              //  This is what you accomplished in your simple equation.

              //  Assuming their pay is increasing this will only ever offer you a negative value.


              You may instead wish it to read like this for Field Pay3

               

              var P1 = this.getField("Pay1").value;

              var P2 = this.getField("Pay2").value;

              var D1F = 0;

              if (P1<=P2) {

                 D1F = P2-P1}

              else {

                 D1F = P1-P2}

              this.getField("Pay3").value = D1F;

              // This guarantees you will always have a positive value regardless of weather their pay increase or decrease.

               

              Now for your fourth - I dont think your math is correct for what your trying to achieve.

              to reflect a percent - it should be Pay1 divided by Pay2

               

              var P1 = this.getField("Pay1").value;

              var P2 = this.getField("Pay2").value;

                if (P1 === 0) {P1 =1}

                if (P2 === 0) {P2 =1}

              var P3R = P1/P2*100;

              var PERC3NT = 100 - P3R;

              this.getField("Pay4").value = PERC3NT;

              // Assuming the field is titles Pay4.

               

              // if you want a decimal version of the Percent then you simply add 

              var P1 = this.getField("Pay1").value;

              var P2 = this.getField("Pay2").value;

                if (P1 === 0) {P1 =1}

                if (P2 === 0) {P2 =1}

              var P3R = P1/P2*100;

              var PERC3NT = 100 - P3R;

              PERC3NT = PERC3NT / 100;

              this.getField("Pay4").value = PERC3NT;

              // Again assuming the field is titles Pay4.

               

              This displays a few extra steps when it comes to basic math but this example is also to show the basics of code.  Spelling an punctuation is vital.  You can use alternate variables if you so choose  P1, P3R, PERC3NT can be any thing you want so long as it starts with a letter.  You can even use simple algebra terms like x, y, z or a, b, c.  The term "var" is only used once per variable and only to declare that it will be used latter.  this.getField("YourFieldDesignation").value is how you get and send values to desired fields like Pay1.

              • 4. Re: Creating custom calculation script
                gkaiseril MVP & Adobe Community Professional

                Well, you cannot divide by zero!

                 

                At some point if you should get an answer.

                 

                There maybe other issues that you are not aware of and without seeing the form and all the scripts it is pretty hard to resolve the issue.

                 

                Is the order in which the fields are being calculated correct?

                 

                You need to use the custom calculation script and test for a non-zero divisor and then performing the division or take an action as needed for a zero divisor.

                 

                Are you sure your formula is correct?

                 

                To get change in pay based on the starting pay I would have used (pay2 - pay1) / pay1 and use the Percentage format for the field.

                • 5. Re: Creating custom calculation script
                  MrGawain Level 1

                  True you can not divide by 0 but from what I understand they are new to JavaScript.  I did not want to bog down examples with too many If statments.

                   

                  I think it is the basic math that is throwing off the calc.

                   

                  (pay2-pay1)/pay3 should not sum up to a viable percentage of pay increase.

                  • 6. Re: Creating custom calculation script
                    glvmlss01 Level 1

                    Thank you MrGawain. I will try your first suggestion and then continue with your examples.  Maybe I was looking at it for to long to realize that my basic math skills could possibly be off.  I will let you know my results.

                    • 7. Re: Creating custom calculation script
                      glvmlss01 Level 1

                      Right, I will go back through and check my math and make sure the fields are being calculated in the correct order.  Thank you for your help.

                      • 8. Re: Creating custom calculation script
                        gkaiseril MVP & Adobe Community Professional

                        I would not adjust the decimal point for a computed percentage. Instead I would allow the field format to display the percentage and leave the field value as decimal. This is a better idea whenever you need to use the computed percentage in any other computation.

                         

                        For the percent difference field I would use the following custom calculation script in a field with a format of None.

                         

                        var cBase = "pay1"; // field for base value;

                        var cNew = "pay2"; // field for new value;

                        // get the value of the fields as numbers;

                        try{

                        var nBase = Number(this.getField(cBase).value);

                        var nNew = Number(this.getField(cNew).value);

                        } catch(e) {

                        app.alert("Error getting field values\nCheck field names.\n", 0, 2);

                        } finally {

                        var nDiff = 0;

                        var nDelta = 0;

                        nDiff = nNew - nBase;

                        if(nBase != 0) {

                        nDelta = nDiff / nBase;

                        event.value = nDelta;

                        AFPercent_Format(2, 0, 0); // Percent format with 2 decimal places;

                        } else {

                        event.value = "";

                        AFNumber_Format(2, 0, 0); // Number format with 2 decimal places;

                        } // end divisor not zero;

                        } // end try;

                         

                        The script will adjust the formatted value display as needed to suppress a zero value.