Copy link to clipboard
Copied
Trying to determine how to make a gross profit percentage calculation populate in field usgp.
I'm trying to divide usppu/usdcost, multiply by 10, and have the result populate in field usgp. Unfortunately, I'm not getting an error message and the field remains blank.
Note that field usppu populates once uscog (cost of goods) is manually entered and subtracted from usdcost ( i.e. usppu=usdcost-uscog).
Here's what I have so far:
var oFld=this.getField("usppu");
var discount=1;
if(oFld.value!="Off")
{usppu=oFld.value}
if(!this.getField("usppu").value)
{event.value=""}
else
{
event.value=this.getField("usdcost").value/usppu*10;
}
Thank you.
Copy link to clipboard
Copied
You did not define usppu so you probably have an error.
Copy link to clipboard
Copied
The "usppu" field is not a checkbox. I think you must mean for one of the "disco" fields to be used in the "if" statement?
You did not say, but I'm assuming that the script you provided a custom calculation on the "usgp" field? Since this is where you say the result should go. These details are important. Assumptions are bad.
So I would suggest you start out by just doing the straight forward calculation. Then add the discount code.
var oFld=this.getField("usppu");
if(oFld.value && ((Number)oFld.value > 0))
event.value=(this.getField("usdcost").value/(Number)oFld.value)*10;
else
event.value = 0;
Copy link to clipboard
Copied
usgp is a percentage determined with the equation of (usdcost - uscog) / usdcost * 10.
usdcost is determined with usmsrp * discount [checkbox].
How would (Number), which you ecoded, be treated?
Thank you.
Copy link to clipboard
Copied
In the example below, when populated, usgp should result in 60%
Copy link to clipboard
Copied
Just enter the code for the calculation you've already stated above.
Put this code in the custom calculation script for the "usgp" field.
var oFld = this.getField("usdcost");
if(!isNaN(oFld.value) && ((Number)oFld.value > 0)){
var nUSDCost = (Number)oFld.value;
event.value=(nUSDCost - this.getField("uscog").value/nUSDCost) *10;
}
else
event.value = 0;
Copy link to clipboard
Copied
For the cost with discount, use this code in the custom calculatin script for "usdcost"
NOTE: I don't know what the export values are for the "disco" checkbox fields, so I'm just guessing. You'll need to fill in the correct values.
var oFld = this.getField("usmsrp");
if(!isNaN(oFld.value) && ((Number)oFld.value > 0)){
var cDisco = this.getField("disco").value;
if(cDisco = "50") // 50% off
event.value= oFld.value*.5;
if(cDisco = "40") // 40% off
event.value= oFld.value*.4;
else // full value
event.value= oFld.value;
}
else
event.value = 0;
Copy link to clipboard
Copied
Hi Thom, I have never seen the Number method like you have written it: (Number)oFld.value. It's usually Number(oFld.value). I ran a test in the console and I'm getting a syntax error:
var oFld=this.getField("usmsrp");
(Number)oFld.value;
Copy link to clipboard
Copied
I've got C++ on the brain. JS typing uses a different syntax.
Here's the correction
var oFld = this.getField("usmsrp");
if(!isNaN(oFld.value) && (Number(oFld.value) > 0)){
var cDisco = this.getField("disco").value;
if(cDisco = "50") // 50% off
event.value= oFld.value*.5;
if(cDisco = "40") // 40% off
event.value= oFld.value*.4;
else // full value
event.value= oFld.value;
}
else
event.value = 0;