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

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

Guest
May 26, 2017 May 26, 2017

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

TOPICS
Acrobat SDK and JavaScript , Windows
991
Translate
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 ,
May 26, 2017 May 26, 2017

Use this:

event.value = sub*tax/100;

Translate
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
Guest
May 29, 2017 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!

Translate
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 ,
May 29, 2017 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.

Translate
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
Guest
May 29, 2017 May 29, 2017

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

Translate
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 ,
May 29, 2017 May 29, 2017

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.

Translate
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
Guest
May 29, 2017 May 29, 2017

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.

Thanks you for your help!

Translate
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 ,
May 29, 2017 May 29, 2017

What is the value "st" ?

Translate
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 ,
May 30, 2017 May 30, 2017

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

Translate
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
Guest
May 30, 2017 May 30, 2017

try67, how can I send you the file?

Translate
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 ,
May 30, 2017 May 30, 2017

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.

Translate
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
Guest
May 30, 2017 May 30, 2017
Translate
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 ,
May 30, 2017 May 30, 2017

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

Translate
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
Guest
Jun 06, 2017 Jun 06, 2017

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?

Translate
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 ,
Jun 06, 2017 Jun 06, 2017

Yes, but not any more...

Translate
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 ,
Jun 06, 2017 Jun 06, 2017

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.

Translate
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
Guest
Jun 06, 2017 Jun 06, 2017
LATEST

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!

Translate
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