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
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;
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;
Copy link to clipboard
Copied
You're right to clarify assumptions—it's important for accurate scripting, especially in a net salery calculator. Based on your notes, here's a cleaner version of the code using valid syntax and better naming:
Make sure "usppu" is actually the field you're intending to check, not "disco" or another checkbox. Also, confirm that this script is applied to the "usgp" field if that’s where the result should appear. This structure makes it easier to add discount logic later for your net salery calculator .
Copy link to clipboard
Copied
Hi @garrr
A couple of small tweaks should get that gross‑profit percentage showing up:
// Custom Calculation Script for field: usgp
var sell = parseFloat(this.getField("usppu").value) || 0; // unit sell price
var cost = parseFloat(this.getField("usdcost").value) || 0; // unit cost
if (cost === 0) {
event.value = ""; // avoid divide‑by‑zero / blanks
} else {
// Gross‑profit % = (sell – cost) / cost × 100
var gpPct = ((sell - cost) / cost) * 100; // use 100 for a true percent
event.value = util.printf("%.2f%%", gpPct); // formats to 2 decimals + %
}
A few things to keep in mind:
The original script was using a variable usppu without defining it properly, which can cause issues.
Using *100 gives you the actual gross profit percentage (e.g., 35%), whereas *10 would give a scaled-down result.
If the field still doesn’t populate, check the calculation order in the form to ensure usppu is calculated before usgp.
Hope this helps! Let me know if it still doesn’t work.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now