Skip to main content
Participant
April 3, 2025
Answered

Gross Profit calculator issue

  • April 3, 2025
  • 3 replies
  • 957 views

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.

Correct answer jack_4781

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:

 

javascript
var usppu = this.getField("usppu").value; var usdcost = this.getField("usdcost").value; if (usppu && Number(usppu) > 0) { event.value = (usdcost / Number(usppu)) * 10; } else { event.value = 0; }
 

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 .

3 replies

jack_4781Correct answer
Participating Frequently
May 31, 2025

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:

 

javascript
var usppu = this.getField("usppu").value; var usdcost = this.getField("usdcost").value; if (usppu && Number(usppu) > 0) { event.value = (usdcost / Number(usppu)) * 10; } else { event.value = 0; }
 

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 .

garrrAuthor
Participant
July 2, 2025

Thank you so much! Made a couple of tweaks and this version worked pretty well for me.

var usppu = this.getField("usppu").value;
var usdcost = this.getField("usdcost").value;
if (usppu && Number(usppu) > 0) { event.value = (Number(usppu) / Number(usdcost)) * 100; } else { event.value = ""; }

Thom Parker
Community Expert
Community Expert
April 3, 2025

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;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
garrrAuthor
Participant
April 8, 2025

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.

garrrAuthor
Participant
April 8, 2025

In the example below, when populated, usgp should result in 60%

 

PDF Automation Station
Community Expert
Community Expert
April 3, 2025

You did not define usppu so you probably have an error.