Skip to main content
Known Participant
October 18, 2023
Question

Form field with multiple if statements

  • October 18, 2023
  • 3 replies
  • 2025 views

Good afternoon all , im wondering if i could get some help with a script .

I have 5 columns PayRate(a), HoursPerWeek(b), WeeksPerYear(c), MonthsPerYear(d), and Annual Income. 

Annual income is my calculaetd field .

 

What im trying to accomplish is :

if b>0 then a*b

if c>0 then a*c

if d>0 then a*d

if a=" " then " "

This is what i have so far:

var a = Number(this.getField("PayRate").valueAsString);
var b = Number(this.getField("HoursPerWeek").valueAsString);
var c = Number(this.getField("WeeksPerYear").valueAsString);
var d = Number(this.getField("MonthsPerYear").valueAsString);
if (b=>0)
{event.value = a*b ;}
else if (c=>0)
{event.value = a*c ;}
else if (d=>0)
{event.value = a*d ;}

 

 

 

This topic has been closed for replies.

3 replies

CMunro87Author
Known Participant
October 19, 2023

Good morning all , the issue im having with this script is that Annual income is only calculating when B is populated, otherwise if "b" is blank and "c" or "d" is populated nothing happens. 

Bernd Alheit
Community Expert
Community Expert
October 19, 2023

Change all >= to >

CMunro87Author
Known Participant
October 20, 2023

Thank you @Bernd Alheit that worked! 

try67
Community Expert
Community Expert
October 18, 2023

The correct operator is not:

=>

But:

>=

Thom Parker
Community Expert
Community Expert
October 18, 2023

@try67 ,  Excellent Catch!!

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
October 18, 2023

You need to state the specific issue when posting the question.  Otherwise we have to guess. 

 

The code looks good, So I'm assuming you're asking about how to incorporate "a" being empty?

The answer is, add a final else. 

 

var a = Number(this.getField("PayRate").valueAsString);
var b = Number(this.getField("HoursPerWeek").valueAsString);
var c = Number(this.getField("WeeksPerYear").valueAsString);
var d = Number(this.getField("MonthsPerYear").valueAsString);
if (b=>0)
{event.value = a*b ;}
else if (c=>0)
{event.value = a*c ;}
else if (d=>0)
{event.value = a*d ;}
else
   event.value = 0;

 

If you want the Anual income field to be blank when the calculatin is zero, then put that in the formatting. 

 

 

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