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

Help on If statement

New Here ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

500

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

Use if, not If

Use else, not Else

 

Votes

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

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?

 

KMAS73_0-1601497315435.png

 

Votes

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

Change getfield to getField

Votes

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

Post the new version of your full code, as text, not a screenshot, and check the JS Console for errors after you change the value of any field in the file.

Votes

Translate

Translate

Report

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