• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Custom Script calc working until a user alters one of the fields back to blank

Community Beginner ,
Jan 15, 2021 Jan 15, 2021

Copy link to clipboard

Copied

I have an order form for a client (a wholesaler working with dealers) that overall is pretty lengthy, the different dealers are the ones using the provided order form. There are calculations throughout the form, but when we get to the end we have the Subtotal, then a Down-payment field, after the down payment there are some fee fields, then the final Balance due. Here is a screen shot of the fields on the form with some test data. 

 

Screen Shot 2021-01-15 at 6.20.54 PM.png

Ideally they would fill the form out in order, but the feedback my client has received from the dealers, and some recent orders that have come in messed up, is that the end customer would like to know the total cost with fees (Balance Due excluding the down payment), so they will go to that field and delete the number (making the field blank or null) and then the calculation for the total goes crazy. 

 

Here is my calculation for the Balance Due using a custom script: 

 

var Total = this.getField("Total").value;

var Down = this.getField("Down_Payment_Before_Tax").value;

var Fee = this.getField("Fee").value;

var Fee2 = this.getField("Fee_2").value;

var Lift = this.getField("Lift_Fee").value;

if(Total>0) event.value=Total+Down+Fee+Fee2+Lift;

else event.value=null

 

(I am kind of new to this, and just set this up using all the help from other post I found here, so this is probably overly complicated for what I was trying to do.)

 

When they delete the Down payment field to blank/null here is a screen shot of what that looks like:

 

Screen Shot 2021-01-15 at 6.31.09 PM.png

 

The Balance due is now 17,000,500,500,500, I am assuming because in my formula when it is trying to calculate Total (17000) + Down Payment (null) + Fee (500) etc. once the calculation hits the null then it is just concatenating the values rather than trying to add it. 

 

What I have tried: 

I have tried to see if there was a way to have a formula that would define a value=null; event.value=0, some things like that, so if they did delete that Down payment field it would be null and essentially default to 0. 

I have also tried a validation of similar to null=0.

Temp solutions was to create a validation app alert and notify the user that the Field must be 0 or greater, and then made the field required, so they would have to add at least a 0 back in. 

 

I might be looking at this the wrong way, so I am open to suggestions, and I know that it may seem like the placement of the Down Payment seems odd, and the simplest solution would be just to move that below the Balance Due, but there are a lot of other elements to this order form, with payment plans, and certain percentages of the total due based on the total cost of the project, that I have not shown here. 

 

Any help or assistance in thinking through this a different way I am open to. 

TOPICS
JavaScript , PDF forms

Views

319

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 16, 2021 Jan 16, 2021

Try to replace:

if(Total>0) event.value=Total+Down+Fee+Fee2+Lift;

else event.value=null

 

By:

if (Total>0) {event.value = Number(Total) + Number(Down) + Number(Fee) + Number(Fee2) + Number(Lift);}

else {event.value = "";}

 

Votes

Translate

Translate
Community Expert ,
Jan 15, 2021 Jan 15, 2021

Copy link to clipboard

Copied

- Don't use "null" as a value. Use 0 or an empty string ("").

- Convert all values explicitly to numbers before adding them up, to make sure you're not concatenating strings, instead.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

Try to replace:

if(Total>0) event.value=Total+Down+Fee+Fee2+Lift;

else event.value=null

 

By:

if (Total>0) {event.value = Number(Total) + Number(Down) + Number(Fee) + Number(Fee2) + Number(Lift);}

else {event.value = "";}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 16, 2021 Jan 16, 2021

Copy link to clipboard

Copied

LATEST

Thank you for providing this information that worked perfectly! That also helped me to under stand this a little better, I was thinking that in my initial this.getField(XX).value that was defining it as a number, but this now helps me to understand this better for the future. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines