Expand my Community achievements bar.

Referencing and comparing strings in different dynamic subforms with For loop

Avatar

Level 4

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.

I changed the script since then 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;

        }

    }

}

And this doesn't generate errors, but rather, it puts the same total in both fields that appear at the bottom.

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

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

Thanks,

Jo

6 Replies

Avatar

Level 5

Hi Jo,

Having looked at the code and it appears that the problem is caused by this line

this.rawValue = this.rawValue + myTemp2.corresp.rawValue;

The 'this' object is referring to all the instances of the correspTotal for that table and therefore everytime it is run it is adding the totals to all of the fields, I think this needs to be changed to refer to the specific instance of the correspTotal field you are wanting to add the value to.

I am still investigating how to do this but I thought I would let you know my finding so that you would be able to investigate.

Hope this helps

Malcolm

Avatar

Level 5

HI,

I have been able to get something similar to what you wanted working, I have had to change it to run on a button click, this is due to the calculation actually running multiple times and therefore creating incorrect answers in the totals table.

the file is here ( sorry if I messed up the layout a bit) - https://acrobat.com/#d=BTu6GrrnWBKM01*91Q0txg

Here is the changed code so that you can see easily what has been done ( and also for anyone else who has a similar issue)

var repCount = form1.P1._rep.count;

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

// get the total number of different clients

var allClientsCount = this.parent.parent._clientTotals.count;

// create an array to make sure that we reset the total to zero

// but only on the first of each client.

var firstRun = new Array();

for ( var k = 0; k < allClientsCount; k++)

{

    firstRun.push (true);

}

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

{

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

    {

        var tempTotals = null;

       

        // I broke the resolveNodes up just to make it easier to follow.

        var myTemp = xfa.resolveNode( "P1.rep["+x+"]");

        var myTemp2 = xfa.resolveNode ( myTemp.somExpression + ".ItemSet["+i+"]");

        var myTempVar = myTemp2.client.rawValue;

       

        // loop for each possible client and add to the total for that client

        for ( var j = 0; j < allClientsCount; j++)

        {

            var tempTotal = this.parent.parent.resolveNode("clientTotals["+j+"]");

            if (tempTotal.clientName.rawValue == myTempVar )

            {

                // if we have not added to this specific client before, start with the first entry

                if ( firstRun[j] == true)

                {

                    tempTotal.correspTotal.rawValue = myTemp2.corresp.rawValue;

                    // set the array to say that this value has been reset

                    // and therefore every other value should be added on

                    firstRun[j] = false;

                }

                else

                {

                    tempTotal.correspTotal.rawValue += myTemp2.corresp.rawValue;

                }

            }

        }

    }

}

Hopefully this will be useful to you in your final solution.

Malcolm

Avatar

Level 4

Thank you!! It works! I wish there were a way to make it more automated; i.e., the user not having to remember to click the buttons to update the totals at the bottom. But if I can't get it to calculate that way, this way will have to do! I really appreciate your help on this!

One thing I have to do now is get the other seven fields in the "clientTotals" subform to also update when the button is clicked, just like the first column. I tried adding the script to the button in the same manner that you had added the correspTotal, but I'm getting an error that "myTemp2.hcfaTotal is undefined", etc. Any idea where I'm going wrong there?

Thank you again so much!

Jo

Avatar

Level 5

Hi Jo,

As the myTemp2.hcfaTotal, is the field in the 'P1.rep[x]' part of the form it looks like it should just be

myTemp2.hcfa.rawValue;

And therefore the IF statement in my code about should look something like this

if ( firstRun[j] == true)

                {

                    tempTotal.correspTotal.rawValue = myTemp2.corresp.rawValue;

                    tempTotal.hcfaTotal.rawValue = myTemp2.hcfa.rawValue

                    firstRun[j] = false;

                }

                else

                {

                    tempTotal.correspTotal.rawValue += myTemp2.corresp.rawValue;

                    tempTotal.hcfaTotal.rawValue = myTemp2.hcfa.rawValue

                }

Hope this helps

Malcolm

Avatar

Level 4

That did it! I thought that was how it should have been, but I had

tempTotal.hcfaTotal.rawValue = myTemp2.hcfaTotal.rawValue

instead of just

tempTotal.hcfaTotal.rawValue = myTemp2.hcfa.rawValue


Works great now! Thank you so much!

If anyone has any ideas on how to make it automatically total at the bottom, I would love to see it!

Thank you again Malcolm!!

Jo

Avatar

Level 4

Sorry to drag this up again, but my form still isn't working entirely correctly. Malcolm, your form works great until I add another Billing Rep section. Then I get the "myTemp2 is null" error.

I'm cross-posting...hope that's ok:

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 client 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 Niall worked out for me (https://acrobat.com/#d=RTiyr-WmPLyFbjspz*U9gQ), 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