Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Gross Profit calculator issue

New Here ,
Apr 03, 2025 Apr 03, 2025

Trying to determine how to make a gross profit percentage calculation populate in field usgp.

Screen Shot 2025-04-03 at 2.56.49 PM.png

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.

 

Screen Shot 2025-04-03 at 2.50.36 PM.png

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.

TOPICS
JavaScript , PDF forms
449
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Apr 08, 2025 Apr 08, 2025

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;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2025 Apr 03, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2025 Apr 03, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 08, 2025 Apr 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 08, 2025 Apr 08, 2025

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

 

Screen Shot 2025-04-03 at 2.50.36 PM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2025 Apr 08, 2025

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;

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2025 Apr 08, 2025

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;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2025 Apr 08, 2025

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2025 Apr 08, 2025

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;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 31, 2025 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 .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 18, 2025 Jun 18, 2025
LATEST

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:

  • Make sure you're converting field values to numbers using parseFloat(), since field values are strings by default.
  • 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines