Skip to main content
Participating Frequently
April 12, 2025
Answered

Customer calculation: Perform action X if fields contain info, perform Y if fields are blank or null

  • April 12, 2025
  • 2 replies
  • 853 views

Hi all,  

Disclaimer upfront, I am a novice at JavaScript.  I've tried reusing some that has worked for me in past scenarios, and i've searched the internet extensively and just cannot get an answer.  Would appreciate some help!  


In concept, this seems like such a simple problem, so I'm frustrated I can't get it working.  I have a table with a lot of form fields.  Each row is a line item.  Some rows only have a member price and some rows have a member and non-member price.  Then there is a quantity field for the user to enter data.  And a total field.  The total field is working beautifully when there is only one price.  I have the calculation set to do a product of the price * quantity.  The problem I am having is when there are 2 prices.  I need to be able to tell it which price to use.  So, I was trying to add a Custom Calculation script. 

  • If the member number text field has a value OR one of the 3 membership types is > 0, it should use the member price. 
  • Else if the Member Number text field is blank or null AND the 3 membership types are 0, then it should use the non-member price * quantity = total.  
  • I also am not sure if I need to tell it not to do anything if the quantity field for the row the calculation is on is blank.  

Right now it calculates both formulas when i run them independantly.  But even though in the form I only have a value in the quantity field of the row i'm trying to calculate, all the other fields i specified are blank, it seems like it will not move to the else if scenario.  

//Define vars
var MemNum = this.getField("MemberNumber").value;
var AnnualMem = this.getField("AnnualMembershipTotal").value;
var ThreeYr = this.getField("ThreeYearMembershipTotal").value;
var AssociateMem = this.getField("AssociateMemberTotal").value;
var Total = this.getField("AmateurCardTotal").value;
var Qty = this.getField("AmateurCardQuantity").value;
var Member = this.getField("AmateurCardPriceMember").value;
var NonMember = this.getField("AmateurCardPriceNonMember").value;

if ((MemNum !== "") || (!MemNum) || (AnnualMem > 0) || (ThreeYr > 0) || (AssociateMem > 0)) { Total = Member * Qty; }
else if ((MemNum == "") || (MemNum) || (AnnualMem = 0) || (ThreeYr = 0) || (AssociateMem = 0)) { Total = NonMember * Qty;}

 

Correct answer PDF Automation Station

This part of your code will always make the entire statement return true: 

(MemNum !== "") || (!MemNum)

Because it says if MemNum has a value OR MemNum does not have a value.  Also, "is equal to" is expressed with 2 equal signs like this:  AnnualMem==0  Try this:

if(MemNum || AnnualMem > 0 || ThreeYr > 0 || AssociateMem > 0)
{event.value = Member * Qty}
else
{event.value = NonMember * Qty}

 

2 replies

Participant
April 26, 2025

 

Hey, I totally get the frustration, what sounds like a simple "if this, then that" logic turns into something weird when forms and JavaScript get involved.

Looking at your script, I think there are a couple of things that might be throwing it off: That (!MemNum) check in your first if block actually triggers when the field is empty, not filled. So it might be pulling the logic in the wrong direction. You probably want to check if the field has a value — using MemNum.trim() !== "" usually does the trick for that.

  1. Also, in the else if, it looks like you’re using = instead of ===. That one always bites me — it’s assigning a value, not comparing it. So AnnualMem = 0 actually sets it to 0 instead of checking it.

  2. And just to be safe: make sure all those field values are treated as numbers. Adobe fields return strings by default, so when you're doing math like Member * Qty, it's a good idea to wrap each in parseFloat(...) || 0 to avoid unexpected results.

Here’s how I’d write it:

 

javascript
CopyEdit
var MemNum = this.getField("MemberNumber").value; var AnnualMem = parseFloat(this.getField("AnnualMembershipTotal").value) || 0; var ThreeYr = parseFloat(this.getField("ThreeYearMembershipTotal").value) || 0; var AssociateMem = parseFloat(this.getField("AssociateMemberTotal").value) || 0; var Qty = parseFloat(this.getField("AmateurCardQuantity").value) || 0; var Member = parseFloat(this.getField("AmateurCardPriceMember").value) || 0; var NonMember = parseFloat(this.getField("AmateurCardPriceNonMember").value) || 0; if (MemNum.trim() !== "" || AnnualMem > 0 || ThreeYr > 0 || AssociateMem > 0) { event.value = Member * Qty; } else if (MemNum.trim() === "" && AnnualMem === 0 && ThreeYr === 0 && AssociateMem === 0) { event.value = NonMember * Qty; } else { event.value = 0; }
 

This should cleanly switch between prices based on whether the person qualifies as a member or not.

Also, this kind of logic, choosing between different rates, quantities, and totals, reminds me of something I ran into while building an hours calculator for tracking work time and breaks. If you ever need something like that, where totals shift depending on user input, it's been a real time-saver.

PDF Automation Station
Community Expert
Community Expert
April 12, 2025

This part of your code will always make the entire statement return true: 

(MemNum !== "") || (!MemNum)

Because it says if MemNum has a value OR MemNum does not have a value.  Also, "is equal to" is expressed with 2 equal signs like this:  AnnualMem==0  Try this:

if(MemNum || AnnualMem > 0 || ThreeYr > 0 || AssociateMem > 0)
{event.value = Member * Qty}
else
{event.value = NonMember * Qty}

 

RedrockCAuthor
Participating Frequently
April 12, 2025

Thank you for your assistance!  And for explaining.  I am trying to understand so I can do better next time.  I think I need to go read up and understand better what all and when all to use event.value.  

I have a follow-up question.  I added the new code and it calculates correctly, but Acrobat is saying "The value entered does not match the format of the field [AmateurCardTotal]".  Is this because on the Format tab I selected Number & 2 decimal places & add a $?  Can i not do that, it formatted when using a custom calculation?  

PDF Automation Station
Community Expert
Community Expert
April 12, 2025

What is the calculation?  Is it division?  Dividing by zero returns infinity which is not a number.  If the dividend is entered before the divisor, the calculation runs returning Infinity and the format error is tripped.  FYI, I have a (paid for) JavaScript for Acrobat course:

https://www.pdfautomationstation.com/public/Learn-To-Use-Adobe-Acrobat-Pro-DC-Like-A-Professional.cfm