4 Replies Latest reply on Jul 7, 2016 12:56 PM by jotasprout

    Why is loop skipping fields?

    jotasprout Level 1

      PDF has multiple pages spawned from templates. Purpose of my function is to find all fields with a given prefix and remove those fields to visually clean up final document and reduce file size. For some reason, the function has to be called multiple times to find and destroy all the fields even though they all meet the same criteria. Here's the function:


      // "theSecond" is the target prefix
      function selfDestruct(theSecond) {

          // Create empty variable for target field
          var myFullFieldName;

      // Finds the number of fields in the PDF and loops through them
      for (var i = 0; i < this.numFields; i++) {
        // Gets the name of the current field
        var fieldInSight = this.getNthFieldName(i);

        // Splits the field name into sections and puts them in an array
        var fieldNamePieces = fieldInSight.split(".");

        // Checks if field is a target
        if (fieldNamePieces[2] == theSecond) {
         // If true, it reassembles the field name
         myFullFieldName = fieldNamePieces[0] + "." + fieldNamePieces[1] + "." + fieldNamePieces[2] + "." + fieldNamePieces[3];
         // show me what's being removed
        } // end of if
      } // end of for
      } // end of function


      My argument is either "single", "double", and so on to find and remove the following (the first prefix varies, of course), for example:

      • P2.actions.single.LeftColumn
      • P2.actions.single.RightColumn
      • P2.actions.single.resetTextBox
      • P2.actions.single.TopRow
      • P2.actions.single.BottomRow


      Every time I try it (either in the console or a button executing the script), if there's a group of five (as above), it removes three the first time, a fourth the second time, and the fifth/last field on the third try. Consistently and in the same order each time. I have one "group" with only two fields sharing the same prefix and, each time, it deletes one, then the second one the second time I call the function. I can't figure out why it does this or how it determines the order. If there are two or twenty pages with these fields, it always happens the same way in the same order.


      It gets weirder: If I spawn new pages between calling the function, it leap-frogs ... getting the "fourth" field it missed last time and the first three on the newly spawned pages.


      Please enlighten me. Thanks in advance.