Reducing a List Record in Outsystems mobile 10.

I have a list of 20 records that I want to reduce to 4. The data is coming from a rest API and I can't make a data query for just 4 records.

I am trying to use a for-each loop on the list, starting the index at 4 , with max iterations being (list.length - 4). In each loop I use ListRemove, but am getting the exception where I can't use ListRemove during  iterations. 

I then tried to create a new local variable list, assign the list (so now i have 2 identical lists), iterate the unused list and assign the list to my main list after the iterations, but still getting the exception.

I am using this in the onready module.


I cannot provide any screenshots or omls for business reasons, if there's any additional information required I can provide it.


Thanks

PAULO RICARDO FAGUNDES JUNIOR wrote:

Hello Edward,

If you set in Table Record List out ListRecord the property Line Count with 4, may it help you, however you just will hide the others results because you are still fetching all records from Web Service.
In fact the best way would have be that web service has a pagination and then you'll can able to fetch just you need.


Thanks for the quick reply Paulo, I am however trying to do this in mobile and I don't think I can find the TableRecords. In web the ListRecord has a line count as well and I am essentially trying to do that.

I have found an alternative solution where I actually just add a boolean to my data structure, and add true for 4 objects in my list and then use filter list to get the 4 objects.

Sorry, man.. I realized it right now !

If you create a server action and there you can create a foreach, In this loop you can set the maximum iterations, then you return your list with only 4 records.
Is it possible?  

Hey Edward,

Don't assign the list: that won't create a real clone of the list - it's just a pointer. Use the ListAppendAll action to create a copy of the list, and then you can do ListRemove.

Actually, the ListDuplicate is the proper way to create a copy of a List.

That said, it's far easier to use ListFilter to take a section of a List; since ListFilter is just shorthand for a For Each, it is possible to use CurrentRowNumber. So if you want just the first four records of List MyList, use "MyList.List.CurrentRowNumber < 4" (without the quotes) as condition, and you'll get the first four records. 

Kilian Hekhuis wrote:

Actually, the ListDuplicate is the proper way to create a copy of a List.

That said, it's far easier to use ListFilter to take a section of a List; since ListFilter is just shorthand for a For Each, it is possible to use CurrentRowNumber. So if you want just the first four records of List MyList, use "MyList.List.CurrentRowNumber < 4" (without the quotes) as condition, and you'll get the first four records. 

Didn't know CurrentRowNumber would "update" inside the context of a ListFilter, this is very interesting. You're right, it would be the easiest way for Edward to implement this.

Yeah, I found out a while ago, it can be really handy sometimes.