10 Replies Latest reply: Apr 24, 2012 10:24 AM by joflo723 RSS

    So close but need help comparing strings in different subforms with for loop - Livecycle ES

    joflo723 Community Member

      For some reason I was not able to post this in the Livecycle ES forum, even though I've posted there often in the past.

       

      I have created a form that I almost have working and I'm hoping someone can help me.

       

      We have eight billing teams in our company and each team works anywhere from 1 - 4 different clients. The form is used to track, per Billing Rep, the number of items of mail that each billing r5ep works each day. What I'm needing to add at the bottom is a subform that tracks the totals by client. So I'm trying to get the form to loop through all of the existing subforms (and the subforms within them) and compare a cell int he bottom subform to a cell in the other subforms, which are dynamically added, and then total ONLY those items. My script however is not working.

       

      What I have now is:

       

      var repCount = form1.P1._rep.count

      var itemCount = form1.P1.rep._ItemSet.count

       

      for (x=0; x<repCount; x++)

      {

           for (i=0; i<itemCount; i++)

           {

                if(clientName.rawValue == P1.rep[x].ItemSet[i].client.rawValue)

                {

                this.rawValue = this.rawValue + P1.rep[x].ItemSet[i].corresp.rawValue;

                }

           }

      }

       

      It seems like this should work but I'm getting the following error:

       

      P1.rep[x] is undefined


      So what it looks like is I'm missing is the proper way to reference the objects in these subforms.

       

      The form is here: https://acrobat.com/#d=20gWXZ4sBm4OPV6oO76zSg

      If someone could take a look at it I would be so grateful.

       

      Thanks,

      Jo

        • 1. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
          dohanlon Community Member

          Sorry I cant see your document link - probably have to publish it first. However from looking at the script you are using array notation in javascript to access object collections. The hierarchy object model is not a javascript array but rather a collection of dom objects even though the som expressions use them as accessors in formcalc for example.

           

          clientName.rawValue == P1.rep[x].ItemSet[i].client.rawValue

           

          would be invalid javascript since rep is not an array. Probably would need to be P1.resolveNode("rep[x].ItemSet[i]").client.rawValue

          • 2. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
            joflo723 Community Member

            Thanks for your help. I published it here:

             

            https://acrobat.com/#d=20gWXZ4sBm4OPV6oO76zSg

             

            Can you try again.

             

            Also, I have updated the script as you suggested to this:

             

            var repCount = form1.P1._rep.count

            var itemCount = form1.P1.rep._ItemSet.count

             

            for (var x=0; x < repCount; x++)

            {

                for (var i=0; i < itemCount; i++)   

                {

                    if (P1.resolveNode("rep["+x+"].ItemSet["+i+"]").client.rawValue == clientName.rawValue);

                    {

                    this.rawValue = this.rawValue + form1.P1.resolveNode("rep["+x+"].ItemSet["+i+"]").corresp.rawValue;

                    }

                }

            }

             

            Now, when I run it, it gives me totals, but it puts the same totals into both fields.

            • 3. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
              dohanlon Community Member

              Your if statement has a ; at the end basically making the test redundant and executing the code block following at all times. You also should not do - this.rawValue = this.rawValue + ... since it will keep adding on every calculate. Start from 0 in a local variable and assign it to the rawValue at the end.

              • 4. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                joflo723 Community Member

                Thank you so much! I have it working now...EXCEPT...when I add some numbers to that first column, and then add another rep using the "Add Billing Rep" button, I get an error that says:

                 

                "P1.resolveNode("rep[" + x + "].ItemSet[" + i + "]") is null".

                 

                If I add some additional reps before I enter any numbers, it only includes the most recently added numbers.

                 

                The updated form is here: https://acrobat.com/#d=nLDPPNd7fKXPYsnH2Z*oyA

                 

                Any idea what I'm missing?

                 

                Thank you muchly.

                 

                Jo

                • 5. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                  Niall O'Donovan Community Member

                  Hi,

                   

                  Have a look at this version. https://acrobat.com/#d=xScv0bjZI7SjvjsyG7ZUrQ.

                   

                  I have used a variable to keep track of the subtotal and then apply that after the loops.

                   

                  Also I would not use x as a variable name, as it is a reserved word.

                   

                  Hope that helps,

                   

                  Niall

                  • 6. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                    joflo723 Community Member

                    Thank you for following up with me on this Niall...I really like how automated that is. However, I'm getting an error.  If I add values to the first column for the first billing rep, then add another billing rep, I get the following error:

                     

                    P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null

                    7:XFA:form1[0]:clientTotals[0]:correspTotal[0]:calculate

                    TypeError: P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null

                    7:XFA:form1[0]:clientTotals[0]:correspTotal[0]:calculate

                    P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null

                    7:XFA:form1[0]:clientTotals[1]:correspTotal[0]:calculate

                    TypeError: P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null

                    7:XFA:form1[0]:clientTotals[1]:correspTotal[0]:calculate

                    P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null

                    7:XFA:form1[0]:clientTotals[2]:correspTotal[0]:calculate

                    TypeError: P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null

                    7:XFA:form1[0]:clientTotals[2]:correspTotal[0]:calculate

                     

                    What does this mean?

                     

                    Also, the totals for the billing reps added after the first one are not added to the totals at the bottom...sometimes. It's really strange behavior. Sometimes it will add the totals at the bottom and sometimes it throws an error.

                     

                    Joanne

                    • 7. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                      joflo723 Community Member

                      Here is what I have working so far. I'm getting a big error when I open the file. And some totals for some reason just aren't making it to the grand totals section at the bottom.

                       

                      In this file....https://acrobat.com/#d=cP07gIScGXyacqca6Rcmyg

                      I get these results. It looks like it totals at the bottom for additional billing reps sometimes, but not all the time:

                      Clipboard01.jpg

                      And, in the file which you worked for me Niall, it's throwing the errors I noted above, and also not totalling for any additional billing reps:

                      Clipboard02.jpg

                      I am so at a loss as to how to work this out. I would be extremely grateful if anyone could help me figure out how these glitches are occuring. Any ideas?

                       

                      Thanks,

                       

                      Jo

                      • 8. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                        Niall O'Donovan Community Member

                        Sorry Jo,

                         

                        But I am out of the office this week and can¹t get to have another look at

                        your form. I will have a look as soon as I can.

                         

                        Niall

                        • 9. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                          joflo723 Community Member

                          Thanks Niall. I will patiently wait for your expertise! My supervisor extended the go-live date anyway to May 1st, so I have a little time to *hopefully* figure this out! Thank you in advance for any help you can give when you return.

                           

                          Jo

                          • 10. Re: So close but need help comparing strings in different subforms with for loop - Livecycle ES
                            joflo723 Community Member

                            I don't know that anyone else will ever have a similar problem as what I was having, but I figured I would go ahead and post the solution, just in case....

                             

                            The error I was getting (P1.resolveNode("rep[" + j + "].ItemSet[" + i + "]") is null) was occuring because the variable itemCount needed to be referenced within the repCount For Loop. I also needed to resolve the node for the itemCount, which I wasn't doing.

                             

                            So...my old code was this:

                             

                            var repCount = P1._rep.count;

                            var itemCount = P1.rep._ItemSet.count;   //this part was throwing the error. Needed to use resolveNode...and needed to not declare the variable until inside the first For Loop.

                            var nTotal = 0;

                             

                            for (var j=0; j<repCount; j++) {

                                for (var i=0; i<itemCount; i++)    {

                                    if (P1.resolveNode("rep["+ j +"].ItemSet["+ i +"]").client.rawValue == clientName.rawValue) {

                                        nTotal = nTotal + P1.resolveNode("rep["+ j +"].ItemSet["+ i +"]").corresp.rawValue;

                                    }

                                }

                            }

                            this.rawValue = nTotal;

                             

                            ...which wasn't working because it was trying to reference the count of the ItemSet right up front...but it needed to wait until it got inside the first iteration of the repCount subfields...AND it needed to have resolveNode used.

                             

                            The corrected code is:

                             

                            var repCount = P1._rep.count;

                            this.rawValue = 0;

                             

                            for (var j=0; j<repCount; j++) {

                                var itemCount = P1.resolveNode("rep["+j+"]")._ItemSet.count;  //corrected code...node is resolved and variable is declared within the first subform repCount.

                                for (var i=0; i<itemCount; i++)    {

                                    if (P1.resolveNode("rep["+ j +"].ItemSet["+ i +"]").client.rawValue == clientName.rawValue) {

                                        this.rawValue = this.rawValue + P1.resolveNode("rep["+ j +"].ItemSet["+ i +"]").corresp.rawValue;

                                    }

                                }

                            }

                             

                            ...which now works perfectly...and I'm throwing a party to celebrate! LOL

                             

                            Jo