0

In apex, we get the data of the object through SOQL, about 10,000 records, and we need to display all of these records on the front end (LWC).

I've tried three solutions:

  1. Query all the data through SOQL, and then perform paging display in front end (LWC).

    SELECT ID,NAME FROM ACCOUNT WHERE ……

    Problems faced: Trigger CPU TIME OUT or wait too long.

  2. Perform paging query using OFFSET.

    Problems faced: The maximum 2000 limit is triggered.

  3. Search in batches according to search conditions

    Problems faced: Users don't accept this solution.

Are there other solutions to implement paging queries?

4
  • 3
    A user expecting to view 10000 rows is being naive. They will not be able to make proper use of the data. Look at how list views work in the SF UI. They need to apply filters to find data relevant to what they are trying to do so the amount of data is appropriately reduced.
    – Phil W
    Commented Feb 24, 2023 at 8:45
  • Instead of discussing user behavior, we should discuss whether it is possible to conduct batch queries through specific conditions for paging display? ^.^
    – Zhang Ou
    Commented Feb 24, 2023 at 8:56
  • 1
    I disagree. Doing something that is nonsensical because a naive user thinks it is a good idea is wasting time and money, plus sets a bad precedent.
    – Phil W
    Commented Feb 24, 2023 at 15:11
  • In addition, before you go further with the datatable, generate 10000 records and test how it handles that sort of quantity of records. I suspect that it's not going to handle it well and will be slow to scroll. Commented Feb 24, 2023 at 23:22

1 Answer 1

1

There is a way to do it through the 2nd approach, but not with offset, here is what I suggest:

  • add order by id to your first request, retrieve the first batch of 2000 records (for example), store the last id retrieved
  • perform the second soql query not only with order by id and limit 2000, but also with where-clause condition where id > 'last_retrieved_id'

This way you'll be able to retrieve all records just like with offset mechanism and separate them in different transactions (for example, with the help of @RestResource) to avoid CPU TIME OUT.

Example:

SELECT Id FROM User ORDER BY Id LIMIT 10
// retrieved 0051I000000RwQCQA0 ... 0051I000000S2xKQAS
SELECT Id FROM User WHERE Id > '0051I000000S2xKQAS' ORDER BY Id LIMIT 10
// and so on
5
  • We use this for chucked data loads though only where other ordering is not required.
    – Phil W
    Commented Feb 24, 2023 at 15:12
  • You are right, but you can add additional ordering before Id field, for example, ORDER BY Name, Id, than store last retrieved Name AND id. After that the where-clause would look like this WHERE Name >= 'last_retrieved_name' AND Id > 'last_retrieved_id'. This sort of soql query can be adapted. @PhilW Commented Feb 24, 2023 at 15:28
  • That's also true, though you do then increase complexity in tracking the "continuation point" (which we have also done).
    – Phil W
    Commented Feb 24, 2023 at 16:31
  • I've tried this a few times in the console, and so far it's working. First Page Previous Page Next Page are all feasible, do you have any good examples to get the last page @Mariia Illarionova
    – Zhang Ou
    Commented Feb 28, 2023 at 8:43
  • 1
    The only way I can think of is: calculate how many records should be on the last page (10150 = 2000*5 + 150, for example), make reverse order by and get first 150 records, then output them in reverse order. Commented Feb 28, 2023 at 8:47

You must log in to answer this question.

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