function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DrawloopDrawloop 

Paginate Records using QueryResultIterator in Flex

I would like to be able to start with showing records 1-200 from a particular query, then when a button is clicked show records 201-400. Here is what I have so far:

Code:
apex.batchSize = 200;
var d:QueryResultIterator = new QueryResultIterator(apex, querystr,
 function (so:SObject):Boolean
 {
  var qr:QueryResult = d.queryResult();
  for (var i:int = 0; i < qr.records.length; i++)
  {
   var q:QueryResult = (qr.records[i] ? qr.records[i] : qr.records);
   //display record
  }
  return false;
 }
);

This gets me the first set of records fine but when I run this code a second time it still gives me the first results. What am I missing? Do I have to use an event listener?

Thanks!

Tracy
werewolfwerewolf
First of all, you're not really using the QueryResultIterator you have there.  You're just setting a query result to it and then iterating through that query result later.

I think you may want to think about ditching the iterator and doing it manually (as you are pretty much doing anyway) by calling queryMore when you want the next batch of records.  If you want to be able to scroll back too, you'll want to store that information somewhere other than just the QueryResult object.
DrawloopDrawloop
Here is what I've come up with:

Code:
private var ql:String = '';
if (ql == '')
{
 var d:QueryResultIterator = new QueryResultIterator(apex, querystr,
  function (so:SObject):Boolean
  {
   var qr:QueryResult = d.queryResult();
   ql = qr.queryLocator;
   //display records
   return false;
  }
 );
}
else
{
 apex.queryMore(ql, new AsyncResponder(
  function (qm:QueryResult):void
  {
   ql = qm.queryLocator;
   //display records
  }
 ));
}

I get errors when I try to move the display records code to its own function so I have the same bits of code written out three times. A bit annoying, but it's working!
werewolfwerewolf
Sorry, btw, in my original post I misread your code -- I was conflating the AJAX QueryResultIterator with the Flex one.  Now I understand what you're doing.

The code here still doesn't look quite right -- seems like you may be doing lots of queryMores where you don't need to.  If you're doing the query within the context of the iterator, it should do all the queryMores for you in the background.  What if you ditch the iterator altogether and just go with a straight up query() call with your query, and then queryMore on that?
DrawloopDrawloop
Yeah, that's what I'm doing now. It seemed unneccessary to use the QueryResultIterator since I wasn't doing anything special with it. My code now looks like

Code:
if (querylocator == '')
{
  apex.query(querystr, ...);
}
else
{
  apex.queryMore(querylocator , ...);
}

 
Thanks for your inputs, werewolf!
DowithforceDowithforce

 

Hello  Drawloop,

 

can you please paste your whole code here to help me out?

 

 

I have to do a fetch  lots of records ( in 100000 nos.) and need to do pagination, currently I am using 

 

PHP webservice to fetch records, but handle to paging is difficult.

 

Please guide about this.

 

 

Thanks