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

If else not working

Community Beginner ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

Hi

I spent a lot of time reading and talking with folks.  Finally wrote the script properly

Adobe said no problems but it still doesn't work.

[side question: javascript developer was unsure about event.value and this.getfield.  These are defined by Acrobat?)

Have drop down field with 3 results: Pass, Fail or NA

IF Pass then print averagesupplyrows

If Fail then print averagesupplyrowsleft

IF NA then print NA

see script:

var nSupplyvelocitypassfailnaDD = this.getfield("SupplyvelocitypassfailnaDD").value;

var nAveragesupplyrows = this.getfield("averagesupplyrows").value;

var nAveragesupplyrowsleft = this.getfield("averagesupplyrowslefta").value;

if( SupplyvelocitypassfailnaDD == "Pass" ) {

event.value = nAveragesupplyrows;

}

else if( SupplyvelocitypassfailnaDD == "Fail" ) {

event.value = nAveragesupplyrowslefta;

}

else if ( SupplyvelocitypassfailnaDD == "NA" ) {

event.value = "NA";

}

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.5K

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

correct answers 1 Correct answer

LEGEND , Aug 31, 2018 Aug 31, 2018

The first thing is to check the JavaScript console. Error messages don't pop up, instead they all go in the console. Anything there?

If not, please tell us exactly where you put the script. Each field has many different events, and it's easy to put things in the wrong place.

Votes

Translate

Translate
LEGEND ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

The first thing is to check the JavaScript console. Error messages don't pop up, instead they all go in the console. Anything there?

If not, please tell us exactly where you put the script. Each field has many different events, and it's easy to put things in the wrong place.

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 Beginner ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

Thank you.

I got the console open, no errors that I can find.

The script was put into a field - calculation - custom calculation script.

Both supplyleft and supply rows fields have correctly calculated numbers.

Is that a problem?  Because only the words Pass, Fail and NA are conditional.

The supply average rows are calculated numbers (based on average of supply readings)

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 ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

There must be errors in the console because you made several mistakes in the code.

You used "getfield" instead of "getField", and JS is case-sensitive, so it should have caused something like this to appear in the JS Console:

TypeError: this.getfield is not a function

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
LEGEND ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

Regarding this line:

event.value = nAveragesupplyrowslefta;

Your variable name does not have the "a" at the end.

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 Beginner ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

You guys/gals are great!

So I figured out how to use the console (ok not too complicated)

I fixed the getField

I fixed the a at the end.  And now have this code:

var nSupplyvelocitypassfailnaDD = this.getField("SupplyvelocitypassfailnaDD").value;

var nAveragesupplyrows = this.getField("averagesupplyrows").value;

var nAveragesupplyrowslefta = this.getField("averagesupplyrowslefta").value;

if( SupplyvelocitypassfailnaDD == "Pass" ) {

event.value = nAveragesupplyrows;

}

else if( SupplyvelocitypassfailnaDD == "Fail" ) {

event.value = nAveragesupplyrowslefta;

}

else if ( SupplyvelocitypassfailnaDD == "NA" ) {

event.value = "NA";

}

and these errors, although I think I fixed one of them:

Exception in line 1 of function top_level, script Field:Calculate

TypeError: this.Getfield is not a function

1:Field:CalculateException in line 4 of function top_level, script Field:Calculate

ReferenceError: SupplyvelocitypassfailnaDD is not defined

4:Field:CalculateException in line 4 of function top_level, script Field:Calculate

ReferenceError: SupplyvelocitypassfailnaDD is not defined

4:Field:Calculate

I don't understand why ...DD is not defined?  its a drop down with always 1 thing chosen (default is Pass)

what does 4:Field:Calculate mean?

THANKS!

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 ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

You have to double-check your variable names. You defined a variable called "nSupplyvelocitypassfailnaDD", but then tried to access it using "SupplyvelocitypassfailnaDD" (notice the missing starting "n"). That won't work... I recommend you use more simple variable names. It will help you identify such problems much easier.

Notice that there doesn't have to be any relation between the name of the variable and that of the field.

So you can do this, for example:

var v1 = this.getField("SupplyvelocitypassfailnaDD").value;

var v2 = this.getField("averagesupplyrows").value;

var v3 = this.getField("averagesupplyrowslefta").value;

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 Beginner ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

Hi

Ok made some changes:

var nDD = this.getField("SupplyvelocitypassfailnaDD").value;

var nAvg = this.getField("averagesupplyrows").value;

var nAvglefta = this.getField("averagesupplyrowslefta").value;

if( nDD == "Pass" ) {

event.value = nAvg;

}

else if( nDD == "Fail" ) {

event.value = nAvglefta;

}

else if ( nDD == "NA" ) {

event.value = "NA";

}

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 ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

OK, and is it working now? ...

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 Beginner ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

Its working!

I have a bunch of variations of that theme in this form, so this is great.

Is there a limit to the scripts in a form?  Does it screw things up?

THANK YOU AND ALOHA!

ben

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 ,
Aug 31, 2018 Aug 31, 2018

Copy link to clipboard

Copied

Honestly, once you've got more than one "else if" statement, you might want to consider a "switch" statement instead. I find it far easier to read.

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
LEGEND ,
Sep 01, 2018 Sep 01, 2018

Copy link to clipboard

Copied

It is also easier to maintain and rest the order of the testing and provides for a value when there has been no match to any of the test conditions.

var nDD = this.getField("SupplyvelocitypassfailnaDD").value;

var nAvg = this.getField("averagesupplyrows").value;

var nAvglefta = this.getField("averagesupplyrowslefta").value;

switch(nDD) {

     case "Pass":

        event.value = nAvg;

        break;

     case "Fail":

         event.value = nAvglefta;

         break;

     case ""NA":

         event.value = "NA";

         break;

     default:

        event.value = "";

        break;

     }

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 01, 2018 Sep 01, 2018

Copy link to clipboard

Copied

What's wrong with how you have it now?

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 01, 2018 Sep 01, 2018

Copy link to clipboard

Copied

LATEST

There's nothing wrong with code that works. Nothing. However, I'm one of those "teach a man to fish" kind of guys and a chain of "else if" statements is just more efficiently expressed as a "switch" statement.

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