Skip to main content
May 26, 2017
Question

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

  • May 26, 2017
  • 1 reply
  • 1082 views

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

}

I am about to give up, please help!!!

Thank you in advance

This topic has been closed for replies.

1 reply

Bernd Alheit
Community Expert
Community Expert
May 27, 2017

Use this:

event.value = sub*tax/100;

May 29, 2017

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?

Thank you in advance!

try67
Community Expert
Community Expert
May 29, 2017

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.