Skip to main content
Known Participant
April 28, 2019
Answered

if/then summation not working properly

  • April 28, 2019
  • 1 reply
  • 928 views

In an order form, I would like a field to stay blank unless a quantity has been purchased, so I am writing code instead of using the basic option of selecting fields (which works). I need a total number of t-shirts purchased based on five sizes. At first, it seems to work, but with no reason I can detect, it stops adding the numbers and instead treats them like a string. Example: 5xs + 5m gives me 10total. Then if I add another 5l, I get 555 as the sum. (The shirt sizes and quantities are irrelevant to the outcome.) Obviously, there are more advanced ways of doing this that go way above my level of knowledge, but can anyone tell me where the problem is?

var qty1 = this.getField("qTYikmpxs").value;

var qty2 = this.getField("qTYikmps").value;

var qty3 = this.getField("qTYikmpm").value;

var qty4 = this.getField("qTYikmpl").value;

var qty5 = this.getField("qTYikmpxl").value;

if( (qty1 + qty2 + qty3 + qty4 + qty5) > 0) event.value = qty1 + qty2 + qty3 + qty4 + qty5;

else event.value = "";

The second recurring problem is that the first purchase field on the form stays blank in the total field until a second quantity ANYWHERE else on the form is selected. It doesn't matter if it is the product immediately below or on another page; only when something else is selected does the product show up.

Quantity can be any number; Price is set as blank until a quantity above zero is entered; TOTAL is the product of both fields. Same formula in every similar TOTAL cell (modified for product name, of course), and it works everywhere immediately, and on first line when more data is entered. I have a 'clear form' button at the top of the page, but see no reason for it to complicate things.

Thanks for any help.

This topic has been closed for replies.
Correct answer try67

Change each line like this:

var qty1 = Number(this.getField("qTYikmpxs").valueAsString);

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 28, 2019

Change each line like this:

var qty1 = Number(this.getField("qTYikmpxs").valueAsString);

Known Participant
April 28, 2019

That worked perfectly for the first problem. Thank you. Much appreciated.

Any ideas on why the TOTAL field on the first product remains empty until another quantity is entered anywhere else?

Known Participant
April 28, 2019

RE: TOTAL on first product problem

Here is my code, asking for the product of quantity and price, but I have the same problem even if I use the first calculation option of simply selecting the field names

var qty = this.getField("qikmb").value;

var pri = this.getField("pikmb").value;

if (qty > 0 ) event.value = qty*pri;

else event.value = "";