Expand my Community achievements bar.

SOLVED

Add n Instances of Fragment

Avatar

Level 2

Please forgive me, I'm very new to the world of LiveCycle and JavaScript, and flying by the seat of my pants.

Pretty basic form requesting information.  Starts off with a numeric field where a user enters a value. Then I've got a fragment, which is just a collection of the same fields.  What I'd like to do is have n repetitions of that fragment based upon the value of the numeric field.  (Which is why I went with a fragment, it's the same stuff over and over.  To me, the documentation on LiveCycle says that such a situation is exactly what fragments are for. And since table sections *can't* be repeated via the addInstance, I figured a fragment would be quick and easy.)

For the life of me, I just can't get it.  My code, if interested is:

var nCount = xfa.resolveNode("Board.Sub.NumericField26").rawValue;

var nItems = Board.InsertionPoint.Sub1._BoardMember.count

if (nCount > nItems)

{

    var nInstance = nCount - nItems

    for(i=1; nInstance; i++)

    {

        Board.InsertionPoint.Sub1._BoardMember.addInstance(1)

    }

}

nCount works, nItems works, nInstance works, but it just isn't adding additional instances.  The longer goal is to have it remove instances as well if the numeric field changes to a lower value. But first things first.

I did find some great information about predicates over here.  I'd love to have such optimized code, but I'm struggling with even the more basic stuff.  VBA in Excel is much more comfortable to me.

Thanks in advance for any help with what has to be something pretty dang basic.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hey,

Just a thought. I think your "if" isn't needed. The "for" checks that. And,

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

or

for(i=1; i<nInstance+1; i++)

is what you need, right?

Stephen

PS .count returns an integer of the number of instances--not zero based. If there are 5 instances, it returns 5 not 4.

View solution in original post

6 Replies

Avatar

Level 7

Hi,

You're using the xfa.resolveNode() method to locate and determine the nCount however, you aren't using it for adding the instance. If your script can't find its way without xfa.resolveNode() then maybe it can't find its way to addInstance(1) without xfa.resolveNode()

So, try:

xfa.resolveNode("Board.InsertionPoint.Sub1.BoardMember").instanceManager.addInstance(1)

or

this.resolveNode("Board.InsertionPoint.Sub1.BoardMember").instanceManager.addInstance(1)

depending on where the search should start. You may only need:

     this.resolveNode("BoardMember").instanceManager.addInstance(1)

or

     this.resolveNode("Sub1.BoardMember").instanceManager.addInstance(1)

Good luck!

Stephen

Avatar

Level 2

Thanks, Stephen.

Wasn't quite it, but your response gave me the desire to look at it all again.  Glaring error:

for(i=1; i=nInstance; i++)

That should be:

for(i=1; i<nInstance; i++)

Trouble is, I'm off by 1 somehow, which is easy enough to code in, but I should have a better understanding of why.  I'm guessing it's due to instances starting at [0], but with the .count, I would think that such a count would include all instances, and not just start at [1], which would actually be the 2nd instance.

Anyway, glaring error found, and it functions as it should now.  So thnk for giving me a bit of hope and getting me to look at the code again.  Silly mistake!!

Now to try and optimize and progress.

Avatar

Correct answer by
Level 7

Hey,

Just a thought. I think your "if" isn't needed. The "for" checks that. And,

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

or

for(i=1; i<nInstance+1; i++)

is what you need, right?

Stephen

PS .count returns an integer of the number of instances--not zero based. If there are 5 instances, it returns 5 not 4.

Avatar

Level 2

In the above, the 'IF' isn't needed, per se.  However, the overall goal is to be able to remove instances of the fragment as well, using the standard if...else structure.  Of course, I'd have to nest that in an if to determine if they are equal and I neither need more nor less.

I do like your last suggestion though!  Perfect way to do it.

Avatar

Level 7

One last thought:

for (i = nInstance; i<nCount+1; i++)

Avatar

Level 2

Hadn't thought of that, I may have to give that a try as well.  I was hoping to get i= instead of i<+1, but with the = it just seems to hang and get caught in an infinite loop.  Not sure why, as it works with VBA for Excel