duplicating records in a loop

Hi

I am trying to run a loop of line item type records and for each record i want to create a duplicate of it and then set values into some of the duplicated records fields.

Have tried a number of times with no luck as i’m getting infinite loops created if i duplicate a record inside a loop.

code so far:

for ( var i = 1 ; i <= purchases_to_lineitems.getSize() ; i++ )
{
var iRecord = purchases_to_lineitems.getRecord(i);
forms.lines_in.controller.duplicateRecord(iRecord);
}

would really appreciate some help.

Hi rodneysieb,

You should be carefull with getSize()… it gets incremented when you add a record so you will loop forever if you use it as end point of a loop where you are incrementing records…

Also be carefull with .getRecord(i)… it gets the record fields of recordindex i . These fields are then usables with iRrecord.fieldx . Move sample code or search in documentation for more detail.

For solve your problem one idea can be:

var max_record = purchases_to_lineitems.getSize();
for ( var i = 1 ; i <= max_record ; i++ )
{
	purchases_to_lineitems..duplicateRecord(i,false,true);
	purchases_to_lineitems.your_field = your_data_here;	
	purchases_to_lineitems.your_field2 = your_data2_here;	
}
controller.saveData();

Note that .getSize() of related records is first loaded with only 40 records…so… if you expect bigger datasets… you better load max_record with something like:

var max_record = databaseManager.getFoundSetCount(purchases_to_lineitems)

Thanks so much Enrico!

Your code got me going straight away without a problem. i just needed to get started. i’ll take your advice on the max record issue too and change the few other loops i’ve created.

thanks again,