16 Replies Latest reply on Jun 6, 2017 4:21 PM by nrodri

# Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Hello java community!

I have been researching for days like crazy and breaking my brain without any resolution to this. I am fairly new to javascript so I am hoping someone can help me.

Field names and values

Seller Type: "Individual", "Commercial"

Province: "AB", "SK", "YK", etc - each of the provinces have their appropriate tax rate as export value e.g. "AB" is 5, "SK" is 10 etc.

Sales Tax: based on the export value of the province selected.

Subtotal: sum of amounts

Trying to achieve this calculation:

If (SellerType="Commercial") then Subtotal*(SalesTax/100)

else

if (SellerType="Individual") then 0

My code right now is:

var st = getField("SellerType").value;

var tax = getField("SalesTax").value;

var sub = getField("Subtotal").value;

if (st==="Commercial"){

event.value = sub.value*(tax.value/100);

} else {

event.value = 0

}

• ###### 1. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Use this:

event.value = sub*tax/100;

• ###### 2. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Hi Bernd,

Thank you for the help, unfortunately it's not working still. This is how I modified the code:

var st = getField("SellerType").value;

var tax = getField("SalesTax").value;

var sub = getField("Subtotal").value;

if (st==="Commercial"){

event.value = sub*tax/100;

} else {

event.value = 0

}

the result on the field stays as 0 even when st reads 'commercial'.

any other suggestions?

• ###### 3. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Use this code:

var st = this.getField("SellerType").valueAsString;
var tax = Number(this.getField("SalesTax").valueAsString);
var sub = Number(this.getField("Subtotal").valueAsString);
if (st=="Commercial"){
event.value = (sub*tax)/100;
} else {
event.value = 0
}

If it's still not working add a command to print the value of "st" to the console, to make sure it's what you're expecting it to be.

• ###### 4. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Hi try67 - thanks for the help! still not working though!

In the screenshot below (I hope you can see it ok) I am trying to program the SalesTotal Field.

If Seller Type is commercial then calculate tax based on province, otherwise display \$0 tax.

I modified the code to this (in case you can't read it from the screenshot):

var st = this.getField("SellerType").valueAsString;

var tax = Number(this.getField("SalesTax").valueAsString);

var sub = Number(this.getField("Subtotal").valueAsString);

if (st=="Commercial"){

event.value = sub*(tax/100);

} else {

event.value = 0

}

Still not working

• ###### 5. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

JS is case-sensitive. You need to enter the field names EXACTLY as they appear in your form, so use "subtotal" instead of "Subtotal"...

Also, you should really make it a habit to check the JS Console for error messages. There's bound to be one there notifying you of this mistake. Go to Edit - Preferences - JavaScript and tick the option to show the console on warnings and errors.

• ###### 6. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

These are the errors that show up:

TypeError: this.getField(...) is null

1:Field:Calculate

TypeError: this.getField(...) is null

1:Field:Calculate

TypeError: getField(...) is null

3:Field:Calculate

TypeError: this.getField(...) is null

Not sure how to fix them. I changed my code to this (didnt notice the typo - thanks for that!):

var st = this.getField("SellerType").valueAsString;

var tax = Number(this.getField("SalesTax").valueAsString);

var sub = Number(this.getField("subtotal").valueAsString);

if (st=="Commercial"){

event.value = (sub*(tax/100));

} else {

event.value = 0

}

Result continues to be 0.

• ###### 7. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

What is the value "st" ?

• ###### 8. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

I'll need to see the actual file to be able to help you further.

• ###### 9. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

try67, how can I send you the file?

• ###### 10. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

You can upload it to a file-sharing website (Dropbox, Google Drive, Adobe Cloud, etc.) and post the link to it here, or you can email it to me directly at try6767 at gmail.com.

• ###### 12. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

You have yet another type-o in your code. The field is called "SellerType", not "Seller Type"....

• ###### 13. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

No, there's no typo

var st = this.getField("SellerType").valueAsString;
var tax = Number(this.getField("SalesTax").valueAsString);
var sub = Number(this.getField("subtotal").valueAsString);
if (st=="Commercial"){
event.value = (sub*(tax/100));
} else {
event.value = 0
}

Same errors show:

TypeError: this.getField(...) is null

1:Field:Calculate

TypeError: this.getField(...) is null

1:Field:Calculate

where you able to access the file?

• ###### 14. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Yes, but not any more...

• ###### 15. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

The link you posted is broken, but I fixed it manually and got access to the file. And yes, there is a type-o, but not where you think.

It's in the custom calculation script of SellerType. That's the code you have there:

event.value = this.getField("Seller Type").valueAsString;

This code is wrong, and doesn't make sense at all... You're trying to copy the field's value into itself. That can't work.

• ###### 16. Re: Calculate sales tax from different fields based on text field Seller Type (If Else Statement)

Hi try67,

After taking a break from it and doing some research I finally got it to work!!!

The code was correct, the error was in a referenced field. The export values for that field where not matching the code's instruction. I changed that and now it works beautifully!

thank you for all your help!