Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

addInstance in loop

Avatar

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
nvdm1
Level 1

28-06-2012

Dear adobe experts,

I have a problem with the addInstance property.

I need to add a variable number of instances of a subform.

The code I've written in JavaScript on the initialize event is:

//  add needed nr of instances to the screen.

var length = xfa.record.INFRASTR.nodes.length/21;

length = Math.floor(length);

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

{

       this.instanceManager.addInstance(1);

}

When I run this code for var length = 1( after rouding down), this works perfect and 1 instance of the subform is added to the form. However as soon as the var length contains a value => '2' (meaning the loop is executed at least a second time) my entire form seems to be stuck in an endless loop.

This subform is set to flowed and the repeat subform flag is checked. The entire form is marked as a dynamic form. So I think all the necessary parameter for a repeatable subform are set.

Can anyone help me solve this problem?

Kind regards,

Niels

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 10
Level 8
Jono_Moore
Level 8

Likes

109 likes

Total Posts

1,151 posts

Correct Reply

144 solutions
Top badges earned
Validate 10
Validate 1
Boost 50
Boost 5
Boost 3
View profile

Avatar
Validate 10
Level 8
Jono_Moore
Level 8

Likes

109 likes

Total Posts

1,151 posts

Correct Reply

144 solutions
Top badges earned
Validate 10
Validate 1
Boost 50
Boost 5
Boost 3
View profile
Jono_Moore
Level 8

28-06-2012

Also, instead of a loop you can just set the number of instances.

this.instanceManager.setInstances(number);

or

this.instanceManager.count = number;

Answers (5)

Answers (5)

Avatar

Avatar
Validate 1
Level 10
Niall_O_Donovan
Level 10

Likes

646 likes

Total Posts

4,352 posts

Correct Reply

786 solutions
Top badges earned
Validate 1
Boost 500
Boost 50
Boost 5
Boost 3
View profile

Avatar
Validate 1
Level 10
Niall_O_Donovan
Level 10

Likes

646 likes

Total Posts

4,352 posts

Correct Reply

786 solutions
Top badges earned
Validate 1
Boost 500
Boost 50
Boost 5
Boost 3
View profile
Niall_O_Donovan
Level 10

29-06-2012

Hi,

First off, Jono is right, the setInstance() is another method that would work very well here.

Because you have the script in the repeating object's initialize event (I can see that from the use of "this"), it means that it fires each time a new instance is created. This is what it causing the infinite loop.

Script fires:

     > creates a new instance:

          > which causes initialize event to fire:

               > creates a new instance:

                    > etc...

If you want something to happen when the form is opened then the docReady event is a good choice. However be warned, while this event normally only fires once, if the form is Reader Enabled (at least through Acrobat), then the event fires twice.

See discussions here:

http://forums.adobe.com/message/3028586#3028586

http://forums.adobe.com/message/2620119#2620119

Also I would think that you would need to wrap the script in an if statement. Otherwise every time the form is opened, it is going to add more instances.

Hope that helps,

Niall

Testing links...

Message was edited by: Niall O\'Donovan

Avatar

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
nvdm1
Level 1

01-07-2012

Thanks guys! The setInstance() did the job.

The intialize event explanation will be very usefull in the future.

Avatar

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
nvdm1
Level 1

29-06-2012

Thanks Jono,

I will try this. It's better not to use a loop. I didn't know the setInstances was available. Can I use this on my initialize event? Or do I need to use the docReady event?

I'll try this first thing on Monday because today (and the weekend) I don't have access to the laptop on which I create my form.

Avatar

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile

Avatar
Validate 1
Level 1
nvdm1
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Affirm 1
View profile
nvdm1
Level 1

29-06-2012

Thanks Niall for the quick respone!

There is no specific reason to add this code in the initialize event. I just figured when the form is opened I need to add my instances.

Avatar

Avatar
Validate 1
Level 10
Niall_O_Donovan
Level 10

Likes

646 likes

Total Posts

4,352 posts

Correct Reply

786 solutions
Top badges earned
Validate 1
Boost 500
Boost 50
Boost 5
Boost 3
View profile

Avatar
Validate 1
Level 10
Niall_O_Donovan
Level 10

Likes

646 likes

Total Posts

4,352 posts

Correct Reply

786 solutions
Top badges earned
Validate 1
Boost 500
Boost 50
Boost 5
Boost 3
View profile
Niall_O_Donovan
Level 10

28-06-2012

Hi,

The problem is the event that you have used. The initialize event will fire when the new instance of the repeating object is rendered. Therefore the script fires repeatedly.

If you need this to fire when the form is opened, try docReady (with an if statement I suspect). Otherwise elaborate on why you need this script in the initialize event.

Niall