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

Help with "For" Loop... Not Checking All Boxes in Array

Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Hi there! I am having trouble with a script I'm working on and hoping I can get some advice.

I have a form with several fields that are either visible or hidden depending on the state chosen by the user. The user may select different state forms by checking check boxes, then hits a button to append those state forms to the document.

When a user selects their state, a function runs to set their location (this consists of making state specific fields visible, and checking all boxes for each state form by default). The checkbox portion is where I'm running into issues. My loop will only check or uncheck the first box, then stop. Here is the portion of code in question:

var Location = this.getField("LOCATION").value;

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2");

var ME3 = this.getField("ME_3");

var ME4 = this.getField("ME_4");

var ME5 = this.getField("ME_5");

var NH1 = this.getField("NH_1");

var VT1 = this.getField("VT_1");

var VT2 = this.getField("VT_2");

var allChecks = [ME1, ME2, ME3, ME4, ME5, NH1, VT1, VT2];

var StateChecks = [];

//Loop through fields and push to new array depending on state chosen

for (i = 0; i < allChecks.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") {

        if (allChecks.name.indexOf("ME") !== -1) {

            StateChecks.push(allChecks);

        }

    }

}

for (i=0; i < allChecks.length; i++) {

    allChecks.checkThisBox(0,false);

}

for (i=0; i < StateChecks.length; i++) {

    StateChecks.checkThisBox(0,true);

}

}

In testing snippets of this code in another document, I was able to get the following to work properly and check all three boxes.

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2");

var ME3 = this.getField("ME_3");

var allChecks = [ME1, ME2, ME3];

for (i=0; i < allChecks.length; i++) {

allChecks.checkThisBox(0,true);

}

However, even testing just that small snippet in the current form I'm working on, it still only checks one of the boxes.

Any advice would be greatly appreciated

TOPICS
Acrobat SDK and JavaScript , Windows

Views

901

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

I don't understand the logic behind your code. The fields that don't have "ME" will always be un-ticked, no matter the value of Location... What's the point of that? Could you maybe explain (in words) what you're trying to achieve, first?

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
Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Hi there - apologies for the confusion - I tried to only show the most relevant areas of the code and in so doing didn't fully explain that I already have "KPOR" chosen as a location in my document while I'm testing in the console. The results are the same no matter which location I choose - only one box gets checked (either ME_1, NH_1 or VT_1).

All state specific fields (which consist of both checkboxes and buttons) are hidden prior to choosing a location, and need to be toggled to "visible" / toggled to be checked if they are checkboxes. I start by hiding all the state fields and unchecking all boxes - just in case something has hiccuped up until this point - then displaying/checking only the fields that pertain to that state.

Here is the entire snippet...

var Location = this.getField("LOCATION").value;

var MEHead1 = this.getField("ME_HEADER1");

var MEHead2 = this.getField("ME_HEADER2");

var MEForms = this.getField("ME_FORMS");

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2");

var ME3 = this.getField("ME_3");

var ME4 = this.getField("ME_4");

var ME5 = this.getField("ME_5");

var NHHead1 = this.getField("NH_HEADER1");

var NHHead2 = this.getField("NH_HEADER2");

var NHForms = this.getField("NH_FORMS");

var NH1 = this.getField("NH_1");

var VTHead1 = this.getField("VT_HEADER1");

var VTHead2 = this.getField("VT_HEADER2");

var VTForms = this.getField("VT_FORMS");

var VT1 = this.getField("VT_1");

var VT2 = this.getField("VT_2");

var allFields = [MEHead1, MEHead2, MEForms, ME1, ME2, ME3, ME4, ME5, NHHead1, NHHead2, NHForms, NH1, VTHead1, VTHead2, VTForms, VT1, VT2];

var allChecks = [ME1, ME2, ME3, ME4, ME5, NH1, VT1, VT2];

var StateFields = [];

var StateChecks = [];

//Loop through fields and push to new array depending on state chosen

for (i = 0; i < allFields.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") {

        if (allFields.name.indexOf("ME") !== -1) {

            StateFields.push(allFields);

        }

    } else if (Location == "KCON") {

        if (allFields.name.indexOf("NH") !== -1) {

            StateFields.push(allFields);

        }

    } else if (Location == "KBUR" || Location == "KRUT") {

        if (allFields.name.indexOf("VT") !== -1) {

            StateFields.push(allFields);

        }

    }

}

for (i = 0; i < allChecks.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") {

        if (allChecks.name.indexOf("ME") !== -1) {

            StateChecks.push(allChecks);

        }

    } else if (Location == "KCON") {

        if (allChecks.name.indexOf("NH") !== -1) {

            StateChecks.push(allChecks);

        }

    } else if (Location == "KBUR" || Location == "KRUT") {

        if (allChecks.name.indexOf("VT") !== -1) {

            StateChecks.push(allChecks);

        }

    }

}

for (i=0; i < allFields.length; i++) {

    allFields.display = display.hidden;

}

for (i=0; i < allChecks.length; i++) {

    allChecks.checkThisBox(0,false);

}

for (i=0; i < StateFields.length; i++) {

    StateFields.display = display.visible;

}

for (i=0; i < StateChecks.length; i++) {

    StateChecks.checkThisBox(0,true);

}

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

You can improve the code, but it seems like it should work.

Are there any error messages in the JS Console when you run it?

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
Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Unfortunately, no error messages. Can't seem to figure out where this is going wrong. It toggles visibility appropriately, but won't check all the boxes.

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Can you share the file with us (via Dropbox, Google Drive, Adobe Document Cloud, etc.)?

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
Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

I was preparing the form to share it; and realized that wouldn't help at all because the code is now working in the page I extracted. This leads me to believe something else in the full version of the document is causing issues... but I unfortunately cannot share the entire document as it contains sensitive information.

I rewrote the code a little bit - as you are right, it could use some improvements - but I'm still having issues and found something funky in my tests.

I'm only using one state as an example; assume that the value of Location is "KPOR".

var Location = this.getField("LOCATION").value; 

 

var MEHead1 = this.getField("ME_HEADER1"); 

var MEHead2 = this.getField("ME_HEADER2"); 

var MEForms = this.getField("ME_FORMS"); 

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2"); 

var ME3 = this.getField("ME_3"); 

var ME4 = this.getField("ME_4"); 

var ME5 = this.getField("ME_5"); 

 

var allFields = [MEHead1, MEHead2, MEForms, ME1, ME2, ME3, ME4, ME5];

for (i = 0; i < allFields.length; i++) { 

    if (Location == "KPOR" || Location == "KBAN") { 

        if (allFields.name.indexOf("ME") !== -1) {

           allFields.display = display.visible;

              if (allFields.type == "checkbox") {

                allFields.checkThisBox(0,true);

              }

         }

     }

console.println(Location + " | " + allFields.name + " | " + allFields.name.indexOf("ME") + " | " + allFields.display + " | " + allFields.type + " | " + allFields.value);

}

The console returns (I added the extra spaces for clarity):

KPOR | ME_HEADER1 | 0 | 0 | button   |

KPOR | ME_HEADER2 | 0 | 0 | button   |

KPOR | ME_FORMS   | 0 | 0 | button   |

KPOR | ME_3       | 0 | 1 | checkbox | Off

KPOR | ME_3       | 0 | 1 | checkbox | Off

KPOR | ME_4       | 0 | 0 | checkbox | Yes

KPOR | ME_3       | 0 | 1 | checkbox | Off

KPOR | ME_4       | 0 | 0 | checkbox | Yes

KPOR | ME_5       | 0 | 0 | checkbox | Yes

Judging from the above, it looks like the loop is not iterating over the checkboxes correctly. ME_1 and ME_2 are skipped entirely, and ME_3 and ME_4 appear multiple times.

Any thoughts?

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

You can't use the checkThisBox method on a button field, only radio-buttons or check-boxes

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Therefore, your code should definitely have produced an error message when you used it. Something like this:

GeneralError: Operation failed.

Field.checkThisBox:19:Console undefined:Exec

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
Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Not sure if you saw my last reply... but I added a couple more print lines and am getting some super wacky results.

for (i = 0; i < allFields.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") { 

        if (allFields.name.indexOf("ME") !== -1) {

           console.println(allFields.name);

           allFields.display = display.visible;

              if (allFields.type == "checkbox") {

                allFields.checkThisBox(0,true);

              }

         }

     }

console.println(Location + " | " + allFields.name + " | " + allFields.name.indexOf("ME") + " | " + allFields.display + " | " + allFields.type + " | " + allFields.value);

}

The above returns...

ME_HEADER1

KPOR | ME_HEADER1 | 0 | 0 | button |

ME_HEADER2

KPOR | ME_HEADER2 | 0 | 0 | button |

ME_FORMS

KPOR | ME_FORMS   | 0 | 0 | button |

ME_1

KPOR | ME_3       | 0 | 1 | checkbox | Off

ME_4

KPOR | ME_3       | 0 | 1 | checkbox | Off

ME_4

KPOR | ME_4       | 0 | 0 | checkbox | Yes

ME_5

KPOR | ME_3       | 0 | 1 | checkbox | Off

ME_4

KPOR | ME_4       | 0 | 0 | checkbox | Yes

ME_5

KPOR | ME_5       | 0 | 0 | checkbox | Yes

Why would allFields.name be "ME_1" on line 7 but "ME_3" on line 8? The lines are printed in the same iteration... very confused.

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Can you share the form?

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
Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Unfortunately, the full document contains sensitive information that would take me a long time to remove... and when I extract just the page/fields in question, the code works perfectly fine. Is it possible that there's something in the full version that could be throwing off the array? I've never seen a loop perform this way.

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Can be that there is something wrong with the whole form.

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

LATEST

No, sorry, I didn't see you added that condition. That's fine, then.

I'm afraid that without seeing the full file I can't help you further with this issue, especially since you're saying it works when you extract some pages from it. That leads me to believe you have some sort of conflict, like another script which is also changing those same fields, but in a different way.

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
Explorer ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

I have the checkThisBox method inside of an if statement that only runs if the field type is a checkbox. I don't think that is causing the issue... but I could be wrong?

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 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Looks like that there is something wrong with the array allFields.

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