27 Replies Latest reply on Jun 9, 2015 9:33 AM by Sean.twyman

    JavaScript Calculation Overide

    Sean.twyman Level 1

      I have this javascript that calculates a shipping field based on where the customer is from and if the amount purchased is over $250. There is also a check box that can waive the shipping and that is included. However on occasion we discount the shipping. So i want to be able to manually enter the shipping cost in the shipping field (which is where this code calculates) and i tried this at the end of the code but the calculate just overrides it.

      else {
        event.value = event.value;
      }
      

       

      Here is the full calculate code for the shipping filed.

      var sb = this.getField("Sub Total").value;
      var cn = this.getField("Customer#").valueAsString;
      var sw = this.getField("ShpWaived").value;
      
      
      var startsWith_CA_UT_CO = (cn.substr(0, 2) == "CA") || (cn.substr(0, 2) == "UT") || (cn.substr(0, 2) == "CO");
      
      
      if (sw == "Off" && sb < "250.00" && sb != "0.00") {
          if (startsWith_CA_UT_CO) {
           event.value = 9;
          }
          else {
           event.value = 11;
          }
      }
      else {
        event.value = 0;
      }
      

       

       

      Any help is greatly appreciated.

        • 1. Re: JavaScript Calculation Overide
          try67 MVP & Adobe Community Professional

          This code is problematic: sb < "250.00" && sb != "0.00" ...

          First of all, you can't use mathematical operators on strings. So you should drop the quotes around "250.00" and "0.00" and also probably explicitly convert the field values to numbers, like so:

           

          var sb = +this.getField("Sub Total").value; 

          var cn = this.getField("Customer#").valueAsString; 

          var sw = +this.getField("ShpWaived").value;

          • 2. Re: JavaScript Calculation Overide
            Sean.twyman Level 1

            "Cant" use or shouldn't use?.... This has been working fine with just this code for sometime now.... The only thing I can't figure out is the problem with manually entering a shipping cost in this field and have this code NOT overwrite it.  Will removing the quotes and explicitly converting the field values to number like you suggest move this issue towards a resolution? I am willing to try your suggestion but I don't see how that will lead me in the direction I am looking to go.  Can you explain your reasoning please?

            • 3. Re: JavaScript Calculation Overide
              try67 MVP & Adobe Community Professional

              You can use it if you wish, but you'll get incorrect results. This is not directly related to your question, I just thought I'd help you fix a problem with your code.

              If you want this field to be manually editable then you need to add an if statement that checks the event.source value. If it's the same field then you should do not calculate its value. It would be something like this:

               

              if (event.source && event.source!=event.target) {

                   // perform the calculation

              }

              • 4. Re: JavaScript Calculation Overide
                Sean.twyman Level 1

                So im not sure i understand how it should go into my existing code.... I have changed the code to match your suggestion. But where do integrate your  statement of checking the event.source value?

                 

                var sb = +this.getField("Sub Total").value; 
                var cn = this.getField("Customer#").valueAsString; 
                var sw = +this.getField("ShpWaived").value;
                
                var startsWith_CA_UT_CO = (cn.substr(0, 2) == "CA") || (cn.substr(0, 2) == "UT") || (cn.substr(0, 2) == "CO");  
                   
                if (sw == "Off" && sb < 250.00 && sb != 0.00) {  
                    if (startsWith_CA_UT_CO) {  
                     event.value = 9;  
                    }  
                    else {  
                     event.value = 11;  
                    }  
                }  
                else {  
                  event.value = 0;  
                }  
                
                • 5. Re: JavaScript Calculation Overide
                  try67 MVP & Adobe Community Professional

                  Put your code where it says

                  // perform the calculation

                  in my code above...

                  • 6. Re: JavaScript Calculation Overide
                    Sean.twyman Level 1

                    ok so i did that and it worked however if the the user makes a mistake and needs to go back to the default shipping it doesn't automatically go back. For instance.... I overwrite the shipping calculation and put in 6.95. It stays there but i realize i wasn't supposed to overwrite it so i delete the 6.95 now it shows 0.00 instead of recalculating it back to 9.00 or 11.00 like it should.

                     

                    Here is the code as it stands now:

                    var sb = this.getField("Sub Total").value;
                    var cn = this.getField("Customer#").valueAsString;
                    var sw = this.getField("ShpWaived").value;
                    
                    
                    var startsWith_CA_UT_CO = (cn.substr(0, 2) == "CA") || (cn.substr(0, 2) == "UT") || (cn.substr(0, 2) == "CO");
                    
                    
                    if (event.source && event.source!=event.target) {
                         if (sw == "Off" && sb < 250.00 && sb != 0.00) {    
                        if (startsWith_CA_UT_CO) {    
                         event.value = 9;    
                        }    
                        else {    
                         event.value = 11;    
                        }    
                    }    
                    else {    
                      event.value = 0;    
                    }    
                    
                    
                    }
                    
                    • 7. Re: JavaScript Calculation Overide
                      try67 MVP & Adobe Community Professional

                      Why should it re-calculate? You wanted to be able to edit the field manually so it lets you do that. If you want the value to re-calculate you need to change one of the other fields in the file.

                      • 8. Re: JavaScript Calculation Overide
                        Sean.twyman Level 1

                        try67 wrote:

                         

                        Why should it re-calculate? You wanted to be able to edit the field manually so it lets you do that. If you want the value to re-calculate you need to change one of the other fields in the file.

                        Well Yes it does that, however wouldn't you agree that it should recalculate without having to edit any other fields if the user overwrote the shipping by mistake. That's like punishing the user for making a mistake.....

                        • 9. Re: JavaScript Calculation Overide
                          try67 MVP & Adobe Community Professional

                          Maybe it should, but the code you've used doesn't tell it to do that. If

                          you want you could adjust the code to use the automatic calculation in case

                          the user clears the field.

                           

                          On Wed, May 6, 2015 at 4:47 PM, Sean.twyman <forums_noreply@adobe.com>

                          • 10. Re: JavaScript Calculation Overide
                            Sean.twyman Level 1

                            Ok but how do i modify the code to do that?

                            • 11. Re: JavaScript Calculation Overide
                              Sean.twyman Level 1

                              Any Ideas? Anyone? I could really use some assistance on this.

                              • 12. Re: JavaScript Calculation Overide
                                George_Johnson MVP & Adobe Community Professional

                                Can you post all of the code that's currently in the calculation script?

                                • 13. Re: JavaScript Calculation Overide
                                  Sean.twyman Level 1

                                  Here it is...

                                   

                                  var sb = this.getField("Sub Total").value;  
                                  var cn = this.getField("Customer#").valueAsString;  
                                  var sw = this.getField("ShpWaived").value;  
                                    
                                    
                                  var startsWith_CA_UT_CO = (cn.substr(0, 2) == "CA") || (cn.substr(0, 2) == "UT") || (cn.substr(0, 2) == "CO");  
                                    
                                    
                                  if (event.source && event.source!=event.target) {  
                                       if (sw == "Off" && sb < 250.00 && sb != 0.00) {      
                                      if (startsWith_CA_UT_CO) {      
                                       event.value = 9;      
                                      }      
                                      else {      
                                       event.value = 11;      
                                      }      
                                  }      
                                  else {      
                                  if (sw == "Off" && sb < 250.00 && sb != 0.00) {    
                                      if (startsWith_CA_UT_CO) {    
                                       event.value = 9;    
                                      }    
                                      else {    
                                       event.value = 11;    
                                      }    
                                  }    
                                  else {    
                                    event.value = 0;    
                                  }     
                                  }
                                  }
                                  
                                  • 15. Re: JavaScript Calculation Overide
                                    George_Johnson MVP & Adobe Community Professional

                                    I'll post something in a bit...

                                    • 16. Re: JavaScript Calculation Overide
                                      George_Johnson MVP & Adobe Community Professional

                                      I simplified things a bit in an effort to make it easier to understand:

                                       

                                      (function () {
                                      
                                      
                                        // Get the field values
                                        var sw = getField("ShpWaived").value;   
                                        var sb = +getField("Sub Total").value;   
                                        var cn = getField("Customer#").valueAsString;   
                                      
                                      
                                        // See what the customer number starts with
                                        var startsWith_CA_UT_CO = cn.substr(0, 2) == "CA" || cn.substr(0, 2) == "UT" || cn.substr(0, 2) == "CO";
                                      
                                      
                                        // Calculate the value
                                        var val = 0;
                                      
                                      
                                        if (sw === "Off" && sb < 250 && sb !== 0) {
                                        if (startsWith_CA_UT_CO) {
                                        val = 9;
                                        } else {
                                        val = 11;
                                        }
                                        }
                                      
                                      
                                        // Set this field's value
                                        if (event.source && event.source != event.target) { 
                                        event.value = val;
                                        } else {
                                        // This field triggered this script
                                        // If the value is not empty, override the calculated value
                                        if (!event.value) {
                                        event.value = val;
                                        }
                                        }
                                      
                                      
                                      })();
                                      
                                      
                                      • 17. Re: JavaScript Calculation Overide
                                        George_Johnson MVP & Adobe Community Professional

                                        The indents I had didn't transfer over, but I think you'll be able to understand what's going on. Post again if it doesn't work as it should.

                                        • 18. Re: JavaScript Calculation Overide
                                          Sean.twyman Level 1

                                          So far it works great. Ill post again if i have issues. Thank you so much. I think i understand what its doing thanks to your comments. Very much appreciated.

                                          • 19. Re: JavaScript Calculation Overide
                                            George_Johnson MVP & Adobe Community Professional

                                            That last comment could be phrased another way that is probably more clear:

                                             

                                            // If the user-entered value is empty, use the calculated value

                                            // otherwise, use the user-entered value

                                            • 20. Re: JavaScript Calculation Overide
                                              Sean.twyman Level 1

                                              OK so after using your script for a few days i have tried to adapt it to another form i have (very similar) but i am having trouble with both versions. First let me ask you about your script..

                                              (function () {
                                                // Get the field values
                                                var sw = getField("ShpW").value;    
                                                var sb = +getField("SubT").value;    
                                                var cn = getField("Cust#").valueAsString;    
                                              
                                                // See what the custom number starts with
                                                var startsWith_CA_UT_CO = cn.substr(0, 2) == "CA" || cn.substr(0, 2) == "UT" || cn.substr(0, 2) == "CO";
                                              
                                                // Calculate the value
                                                var val = 0;
                                              
                                                if (sw === "Off" && sb < 250 && sb !== 0) {
                                                if (startsWith_CA_UT_CO) {
                                                val = 9;
                                                } else {
                                                val = 11;
                                                }
                                                }
                                              
                                                // Set this field's value
                                                if (event.source && event.source != event.target) {  
                                                event.value = val;
                                                } else {
                                              // If the user-entered value is empty, use the calculated value
                                              // otherwise, use the user-entered value
                                                if (!event.value) {
                                                event.value = val;
                                                }
                                                }
                                              })();
                                              

                                              it works however, if user goes back and makes an adjustment that changes the sub total. (like changing the qty of products) the user-entered value (shipping charge) goes back to the calculated value, and must be changed again.

                                               

                                              2nd - i tried to adapt this to another form and am having trouble with the calculated value updating if the user has overwritten the calculated value with their own, then changed their mind.  They will typically just delete the user-entered value, which should cause the calculated value to auto populate but instead, it just shows 0.00.

                                               

                                              here is the javascript for this issue:

                                              (function () {
                                              
                                              // Get the field values
                                                var sw = getField("ShpW").value;    
                                                var sb = getField("SubT").value;    
                                                var cn = getField("Cust#").valueAsString;    
                                              
                                                // Calculate the value
                                                var val = 0;
                                                if (sw === "Off")  {
                                                val = 6.95;
                                                } 
                                                
                                                // Set this field's value
                                                if (event.source && event.source != event.target) {  
                                                event.value = val;
                                                } else {
                                              // If the user-entered value is empty, use the calculated value
                                              // otherwise, use the user-entered value
                                                if (!event.value) {
                                                event.value = val;
                                                }
                                                }
                                              })();
                                              

                                              any assistance is greatly appreciated.

                                              • 21. Re: JavaScript Calculation Overide
                                                George_Johnson MVP & Adobe Community Professional

                                                Would you like it to be set up so that if the user changes either of the three input fields that the recalculation occurs, regardless of whether it was previously overridden?

                                                • 22. Re: JavaScript Calculation Overide
                                                  Sean.twyman Level 1

                                                  If the user overrides the shipping cost then it should stay over ridden regardless if the user changes another field. The only time the recalculation should occur is when the user deletes the value of the shipping field. It should then recalculate to the appropriate amount.

                                                  • 23. Re: JavaScript Calculation Overide
                                                    Sean.twyman Level 1

                                                    @George_Johnson were you able to look into this issue for me?

                                                    • 24. Re: JavaScript Calculation Overide
                                                      George_Johnson MVP & Adobe Community Professional

                                                      I won't be able to deal with this before late today.

                                                      • 25. Re: JavaScript Calculation Overide
                                                        Sean.twyman Level 1

                                                        Can anyone assist me with this?

                                                        • 26. Re: JavaScript Calculation Overide
                                                          George_Johnson MVP & Adobe Community Professional

                                                          Sorry bout that, I'll post something in a bit...

                                                          • 27. Re: JavaScript Calculation Overide
                                                            Sean.twyman Level 1

                                                            George can you assist me with this?