Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Use this:
event.value = sub*tax/100;
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
What is the value "st" ?
Copy link to clipboard
Copied
I'll need to see the actual file to be able to help you further.
Copy link to clipboard
Copied
try67, how can I send you the file?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
http://https://drive.google.com/file/d/0B4lhIU3E_r1UWUtVTEhrYUNyZ0E/view?usp=sharing
here you go! thank you really so much!
Copy link to clipboard
Copied
You have yet another type-o in your code. The field is called "SellerType", not "Seller Type"....
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Yes, but not any more...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now