Copy link to clipboard
Copied
Calculating Sum - additional fee variable
I want to calculate the sum of 29 fields.
There is a $30 base fee that gets added to the sum of the fields.
However, there are two items in the list that do not get charged that additional 'base fee' of $30 (highlighted in the image)
Right now, I am using this simplified field notation:
(Cost1+Cost2+Cost3+Cost4+Cost5+Cost6+Cost7+Cost8+Cost9+Cost10+Cost11+Cost12+Cost13+Cost14+Cost15+Cost16+Cost17+Cost18+Cost19+Cost20+Cost21+Cost22+Cost23+Cost24+Cost25+Cost26+Cost27+Cost28+Cost29+Cost30+30)
What's the best way for me get the correct calculation?
Copy link to clipboard
Copied
Try this code as the custom calculation script of the total field:
var total = 0;
var addFee = false;
for (var i=1; i<=30; i++) {
var cost = Number(this.getField("Cost"+i).valueAsString);
total+=cost;
if (cost>0 && i!=1 && i!=24) addFee = true;
}
if (addFee) total+=30;
event.value = total;
Copy link to clipboard
Copied
So you want to add the fee if any of the other fields are selected, basically? Do all of these fields have a value, or does the user enter one into them? Does it not also rely on the quantity fields (Cost*Quantity)?
Copy link to clipboard
Copied
The fee only gets added to the sum, not to the items indivuidually. When the user enters a value, the cost for that item is set to calculate. Then the sum of all of the individual fields is calculated. Right now, the "Total Permit Fee" field also adds $30 to the calculation (that's for the 'base fee' charged by the department. Hoever, there are two items in the list that should not be assessed that additional $30 fee.
Copy link to clipboard
Copied
I'm trying to understand how it should work practically, though. Say the user entered qty 1 for Gas Safety only. Then the cost for that field is calculated to be 50 (for example). So the total is just 50. That's clear.
But if they enter "1" for Boiler then the cost for that field becomes 30, say. So what's the total now? Isn't it 50+30+30 (the fee because of Boiler) = 110?
There's no way not to apply the fee to these two items, too, unless they are the only ones that are selected...
Copy link to clipboard
Copied
"There's no way not to apply the fee to these two items, too, unless they are the only ones that are selected..."
That's the part that I want to fix. If the user only selects one (or both) of the highlighted items, how can I prevent the base fee of $30 from being added to the Total Permit Fee field?
Copy link to clipboard
Copied
Try this code as the custom calculation script of the total field:
var total = 0;
var addFee = false;
for (var i=1; i<=30; i++) {
var cost = Number(this.getField("Cost"+i).valueAsString);
total+=cost;
if (cost>0 && i!=1 && i!=24) addFee = true;
}
if (addFee) total+=30;
event.value = total;
Copy link to clipboard
Copied
Thank you! It's perfect!
I appreciate your time to provide this solution. I'm definitely a novice when it comes to using java script for calculations.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now