inserting portal values via a loop

I have a portal located on a form. Based on certian calculations I want to be able to insert calculated values into various records until certian conditions are met.

The problem I am having seems to be going from one portal record to the next. I also need to start the loop with the first record and exit on the last.

The portal record count varies per parent record.
Here is an example:

var x = 0;
while (x <= elements.Order_Line_Items.getMaxRecordIndex())
elements.Order_Line_Items.recordIndex = 1
{
orders_to_orderlineitems_by_orderid.item_cost = x + “Test” ;
x++;
}

I can get the loop to start and stop at the correct count, but I seem to be having a problem getting the incremented result inserted into each portal record, it is just inserting the last result into the first portal record.

Can someone please show me what I am missing?
Thanks,
Erich

Hi Erich,

You do not seem to be telling the method which portal row to update so it is always staying on record 1 !

Try the following - which is untested :

var x = 1; 
while (x <= elements.Order_Line_Items.getMaxRecordIndex()) 
{ 
elements.Order_Line_Items.recordIndex = x //tell the method which record to act on
orders_to_orderlineitems_by_orderid.item_cost = x + "Test" ; 
x++; 
}

I notice that you are setting the item cost field, which is presumably a number of some type, with a string result and this may give you an error when you run it !?

Hope this helps

Harry

Harry,
yes, this is just an example, the data type I will be inserting will be a price from one of three tables depending upon the results of an if statement.

I want to basically perform an if statement on every record in the portal. I was just trying to start with inserting a value into each record.

Thanks

Humm, still having a bit of trouble with this. I was trying to replicate the Go to Portal Row script in FM.

Go to Portal Row (First)
Loop
Go to Portal Row (Next, exit after last)
Insert value
End Loop

Thanks for the help, I got it.

Hi Erich,

I usually use ‘for’ loops for these looping operations where I have an initilaised value and an increment.

This is what I have just tried and it worked in a test solution :

for ( var i = 1 ; i <= elements.Order_Line_Items.getMaxRecordIndex() ; i++ )
{
elements.Order_Line_Items.recordIndex = i
orders_to_orderlineitems_by_orderid.item_cost = i + " Test"
}

Are you matching the correct element to the related field ?
I ask because your portal element is named ‘Order_Line_Items’ and the relationship is named ‘orders_to_orderlineitems_by_orderid’

Are you receiving any error ?
Have you run this under debugger >

Harry

much faster solution in 2.1:

for(var i=1;i<=orders_to_orderlineitems_by_orderid.getSize();i++)
{
var record = orders_to_orderlineitems_by_orderid.getRecord(i);
record.item_cost = i + " Test";
}
controller.saveData();

Excellent,
I was wondering the difference between GetSize and GetMaxRecordIndex.

Thanks for the help

I have question regarding getRecord: how does a “record” behave when used in mass-creation of records in a loop (e.g. when importing a plain text file). How does the loop needs to be created in order to make use of no screen re-paints and loading masses of data?

currently, foundset.duplicateRecord and newRecord will trigger a selection change. (so a paint action)
This is a behaviour that is a legacy from controller.duplicateRecord and newRecord()

We are planning to change that so that you can do this without a selection change:

var record = foundset.getRecord(foundset.newRecord());
or
var record = foundset.getRecord(foundset.duplicateRecord());

in 2.1RC4 the foundset.newRecord and foundset.duplicateRecord methods have an extra boolean: changeSelection.

If you call those with false then the selection won’t be alted and the new record will just be added to the foundst.

Sounds great! Did I get this right:

If I write foundset.newRecord(false) a record is created “in the dark”?

And second question. Can I do this: var record = foundset.newRecord(false) and record then contains that newly created record?

Thanks!!

first: you have to do:

foundset.newRecord(false,false) 

to get what you want

The first false == Don’t add to top and the second don’t change selection.

only doing newRecord(false) only says don’t add to top.

second.

No you can’t do that. Because newRecord gives you the integer index which one is created (can’t change this anymore because of backwards compartiblity)

you have to do this:

var record = foundset.getRecord(foundset.newRecord(false,false))

cool. Thanks a lot!