13 Replies Latest reply: Aug 10, 2012 7:01 AM by GKaiseril RSS

    Simple If-Then Statement

    a.n.bentz Community Member

      Hello,

       

      I'm working on a form and I have a question pertaining to three fields: V1, V3, and V4. I want V4 to be able to be the product of V1 and V3, if those values are filled. If not, I want to be able to manually put a number into V4.

       

      I've researched and tried various different if,then statements, but none seem to be working. Please help!

       

      Thank you so much,

      Amanda

        • 1. Re: Simple If-Then Statement
          MarkWalsh Community Member

          How about using 2 fields, one calculated, and one for user entered value:

           

           

          var v1 = this.getField('V1').valueAsString

          var v2 = this.getField('V3').valueAsString

           

          if (v1 == '' && v2 =='') {

               this.getField('V4_UserEntry').display = display.visible

               this.getField('V4_Calculated').display = display.hidden

          } else {

               this.getField('V4_UserEntry').display = display.hidden

               this.getField('V4_Calculated').display = display.visible

          }

          • 2. Re: Simple If-Then Statement
            MarkWalsh Community Member

            Another idea would be to use a single field, and calculate the value in the format script. Try something like this to see if it works:

             

            var v1 = this.getField('V1').valueAsString

            var v2 = this.getField('V3').valueAsString

             

             

            if (v1 == '' && v2 =='') {

                 event.target.readOnly = false

            } else {

                 var prod = // ... do your calculation here

             

                 event.target.readOnly = true

                 event.value = prod

            }

            • 3. Re: Simple If-Then Statement
              a.n.bentz Community Member

              Thanks Mark!

               

              Unfortuantely the single field calculation did not work.

               

              The two field version is not working for me either, but I didn't know where to put what the calculation would be (V1*V3), so I tried a couple different places and it didn't look like it was working. What would you suggest?

               

              Thank you!

              • 4. Re: Simple If-Then Statement
                MarkWalsh Community Member

                This should work in the Custom Calculation script:

                 

                var v1 = this.getField('V1').valueAsString

                var v2 = this.getField('V3').valueAsString



                if (v1 == '' && v2 =='') {

                     event.target.readonly = false

                } else {

                     var prod = this.getField('V1').value * this.getField('V3').value


                     event.target.readonly = true

                     event.value = prod

                }

                 

                I was using the 'Format' script since it would retain the value the user has entered (the Calculate script will replace that value).

                • 5. Re: Simple If-Then Statement
                  MarkWalsh Community Member

                  This should work: Put the code in my previous message into the Custom Format script, and put the following into the Custom Calculation script:

                   

                  event.value = event.value

                  • 6. Re: Simple If-Then Statement
                    a.n.bentz Community Member

                    No dice. The math will work if field V1 and V3 are filled. However, if V3 is empty and I try to type something into V4 it will display '0' once I leave the field. The value that I do type into V4 will affect other values.

                     

                    Basically V1*V3, when V3 is applicable, will create V4. Other times V4 will be a general number. V4 - V5 - V6=V7

                    The number I tested it with was 150.00 and it shows in V7, just not in V4. So that tells me the value is in the field, it is just not displaying...correct?

                     

                    Another question, is there anyway to have V4 display the number with two decimal points?

                     

                    I so very much appreciate all of your help with this!! :-)

                    • 7. Re: Simple If-Then Statement
                      MarkWalsh Community Member

                      Your initial message stated that you needed that to happen if Both fields were filled. If you only need to do the calculation if V3 is filled with a value, and allow entering a value in V4 oherwise, then change the IF line to:

                       

                       

                       

                      if (v2 =='') {

                      • 8. Re: Simple If-Then Statement
                        MarkWalsh Community Member

                        To round the number to 2 digits, Change this:

                         

                        event.value =prod

                        to this:

                         

                        event.value = Math.round(prod* 100)/100;

                        • 9. Re: Simple If-Then Statement
                          a.n.bentz Community Member

                          Thank you so much!! I appreciate it.

                           

                          I have one last request, hopefully.

                          Can you look over how I have typed the code? I keep getting a Syntax Error : unterminated string literal 3: at line 4. And I just can't see where the error is...I don't know if I have left off a parenthesis or what.

                           

                          var v1=this.getField('V1').valueAsString

                          var v2=this.getField('V2').valueAsString

                          if(v2=="){

                          event.target.readOnly=false

                          }else{

                          var prod=this.getField('V1').value*this.getField('V3').value

                          event.target.readOnly=false

                          event.value=Math.round(prod*100)/100;

                          }

                          • 10. Re: Simple If-Then Statement
                            MarkWalsh Community Member

                            You have 1 double quote, you need to type 2 single quotes. (or 2 double quotes)

                            • 11. Re: Simple If-Then Statement
                              MarkWalsh Community Member

                              Also, 'event.target.readonly' should not be capitalized (That was my mistake in the first script, the 'O' should be lowercase)

                              • 12. Re: Simple If-Then Statement
                                a.n.bentz Community Member

                                It works great!! The only thing missing is that it won't automatically put in the decimal point and the two spaces after. However, everyone can just manually enter those values if they want them.

                                 

                                THANK YOU SO MUCH FOR ALL OF YOUR HELP. And for being so patient with me!

                                • 13. Re: Simple If-Then Statement
                                  GKaiseril ACP/MVPs

                                  I would be careful using JavaScript's Math.round method since there are known issues with it working properly for all values.

                                   

                                  JavaScript Rounding Errors

                                  Round-off error

                                   

                                  Grade School Sample Files look at the Rounding test, it contains a document level funciton that does not suffer from the rounding error common in JavaScript.