Skip to main content
Participant
April 6, 2020
Answered

Form JavaScript help needed: Need to get value of a field (SCORE) and return text (PASS/FAIL)

  • April 6, 2020
  • 1 reply
  • 4170 views

I do not have any JavaScript experience and am hoping that someone can assist me with a Adobe form calculation.
I am creating a Pass/Fail scorecard that will take a value from a field (Calc_OScore) and then return a word (Perfect!, Pass, Fail) based on the score.

Due to my lack of experience and ignorance, my attempts to make this work have been nothing more than copy/paste trial and error from others' scripts and experiences.
I've tried this:

var text ;
if ("Calc_Score" == 16) {
text = "PERFECT!!";
} else if ("Calc_Score" > 11) {
text = "PASS";
} else {
text = "FAIL";
}

And one of these:

var int1=this.getField("E_score").value;
var int2=this.getField("S_score").value;
var int3=this.getField("P_score").value;
var int4=this.getField("C_score").value;
var tSum = Number(int1+int2+int3+int4);
var rVal = 'NULL';
if(tSum===16){
rVal='PERFECT SCORE!';
}else if(tSum>11){
rVal='NOT PERFECT BUT OKAY!';
}else{
rVal='FAIL';
}
return rVal;

 Can anyone help out here? If it makes it easier to understand what I'm trying to accomplish, here's what my formula in Excel looked like: =IFS(F31=16,"PERFECT!",F31>11,"PASS",TRUE,"FAIL"), where "F31" is now "Calc_OScore".

 I appreciate and welcome all feedback! 

This topic has been closed for replies.
Correct answer Thom Parker

Opps, I didn't make enough changes to your code.

Here's the correct version:

var calcVal = this.getField("Calc_Score").value;
if(calcVal == "")
   event.value = "";
if (Number(calcVal) == 16) {
    event.value = "PERFECT!!";
}else if (Number(calcVal) > 11) {
    event.value = "PASS";
} else {
    event.value = "FAIL";
}

1 reply

Thom Parker
Community Expert
Community Expert
April 6, 2020

Does the "Calc_OScore" calculate the sum now?

You are sort of close. Your code has the correct structure, but is missing some important bits.

Youc an find out more about how to write scripts for calculations here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations/

https://www.acrobatusers.com/tutorials/conditional-execution/

 

Put this code into the custom calculation script in the field that displays the text.

var calcVal = this.getField("Calc_Score").value;
if(calcVal == "")
   event.value = "";
if (Number(calcVal) == 16) {
    text = "PERFECT!!";
}else if (Number(calcVal) > 11) {
    text = "PASS";
} else {
    text = "FAIL";
}

 
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
April 6, 2020

@Thom_Parker

Thanks for checking this out for me.

"Calc_Score" is the field that adds up the scores from 4 different sections of the score card.  The Simplified field notation is E_score + S_score + P_score + C_score.  This calculation is working.

"O_Score" is the field that I would like the text to appear.  When I entered the code you provided into the Custom calculation script, it does not appear to have worked.  

In Preview, I updated the scores in each of the 4 sections and confirmed that "Calc_Score" returned the expected value (in this case, 16).  "O_Score" did not change, nor did it return any error.  

 

 

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 6, 2020

Opps, I didn't make enough changes to your code.

Here's the correct version:

var calcVal = this.getField("Calc_Score").value;
if(calcVal == "")
   event.value = "";
if (Number(calcVal) == 16) {
    event.value = "PERFECT!!";
}else if (Number(calcVal) > 11) {
    event.value = "PASS";
} else {
    event.value = "FAIL";
}
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often