Skip to main content
Participant
Sep 30, 2020 6:34 PM UTC
Question

Help on If statement

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 is closed to new replies. Start a new post to keep the conversation going.

7 replies

KMAS73Author
Participant
Sep 30, 2020 8:23 PM UTC

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?

 

 

Translate
Nesa Nurani
Community Expert
Community Expert
Sep 30, 2020 8:48 PM UTC

Change getfield to getField

Translate
Thom Parker
Community Expert
Community Expert
Oct 1, 2020 12:52 AM UTC

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

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
Sep 30, 2020 7:48 PM UTC

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

Translate
Thom Parker
Community Expert
Community Expert
Sep 30, 2020 6:46 PM UTC

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";


Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
Sep 30, 2020 6:37 PM UTC

Use if, not If

Use else, not Else

 

Translate