Skip to main content
Participant
September 30, 2020
Question

Help on If statement

  • September 30, 2020
  • 4 replies
  • 1120 views

I am new to this and i am trying to put a simple if statement into a field based on the value of another field.

 

Can someone help me correct the below?

 

var score =this.getfield("PriorityScore").value;
If ((score <5))event.value="Declined";
If ((score >5)&&(score <=9))event.value="Low";
If ((score >10)&&(score <=17))event.value="Medium";
If ((score >18))event.value="High";
Else event.value= "error";

This topic has been closed for replies.

4 replies

KMAS73Author
Participant
September 30, 2020

Thank you for your help.  I have pasted this into the calculate property of the field(below). But it isn't triggering the calculation. do i need to put it somewhere else?

 

 

Nesa Nurani
Community Expert
Community Expert
September 30, 2020

Change getfield to getField

Thom Parker
Community Expert
Community Expert
October 1, 2020

Good Catch!!  The problems are always in the small details. 

 

KMAS73,  I see this consitently on the forum, i.e., the immediate assumption that if code is not working, then some specific issue such as "it isn't triggering the calculation" is the problem. It is generally a prudent and wise practice to reserver judgement until you've actually debugged the issue.  The first step, as pointed out by Try67, is to check the console window. I would go a step farther and say you should test your code in the console first, before applying it to a field script. 

You'll find a tutorial on the Acrobat JavaScript Console here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
September 30, 2020

In addition to the correct comments above, your code has various "blind spots", such as 5, 10 and 18, which will result in "error"...

Thom Parker
Community Expert
Community Expert
September 30, 2020

Also, you might want to structure these statements as an if block. In this case, it's for efficiency, but it's also a good general practice to keep related condionals together so they don't walk on each other.

  

var score =this.getfield("PriorityScore").value;

if (score <5)event.value="Declined";
else if ((score >5)&&(score <=9))event.value="Low";
else if ((score >10)&&(score <=17))event.value="Medium";
else if (score >18)event.value="High";
else event.value= "error";


Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
September 30, 2020

Use if, not If

Use else, not Else