I have to paginate the records with 10 records per page. I can do this through the limit and offset. But the server call will be expensive if we go for every 10 records.

So I would change the Limit to 100, but the pagination size would be still 10. Once the 100 records are processed, then only I should go for the server to fetch the next set of 100 records on click of Next or previous. Need help in writing the logic for the above especially on click of Next and Previous buttons

  • First questions arise, with how much volume of data you are leading with. And secondly, the logic you can use is by adding some counter variable that if you are one the 90th data call the apex method again, why I am saying to call on 90'th record because when you click for 100'th record then at that point itself next 100 records should get loaded so as to avoid slow loading. Commented Nov 28, 2019 at 13:34
  • @Bhuvnisha as pranav asked, how much is the data volume. Next is why do you want to make server call for this, rather you can handle pagination on the client side itself. Is there any reason that you are opting for server side?
    – samdev
    Commented Nov 28, 2019 at 13:40
  • Hi @samdev , the reason why i am opting for server side is because the data volume is more around the lakhs. so i will not be able to fetch all the data from server at a time and process in client
    – Bhuvnisha
    Commented Nov 28, 2019 at 13:58
  • @PranavChitrans we have to process lakhs of records. Yes with the help of counter, once it reaches 100, i can make the server call to fetch the other set of 100 records on click of next. But lets say i am displaying the records 101 t0 110 records, at this point of time when user clicks on previous, i should get the 1 to 100 records from server and i should display the records from 90 to 100. So need help in writing the logic to display 90 to 100 records
    – Bhuvnisha
    Commented Nov 28, 2019 at 14:17

1 Answer 1



When we have to process data more than 50000, I prefer using CreatedDate which is as an Indexed field in Salesforce by default which I prefer using in my WHERE clause, and on the basis of CreateDate I used to keep on filtering the data from server-side, so one go you can query up to 50000 which I guess will not hit CPU Time LIMIT and also loading of data will not take much time, and then you can break the 50,000 filtered records for 10 or 25 records per page.

As far as your 2nd part of the question: that, what if you are on 101-200 records? So for this, I would suggest not to bother about the previous button just keep the last 100 records in any temporary list whenever the user clicks on the Next button, and for rest, you already have a set of 50,000 records.

I hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .