Pagination In Custom APEX REST Endpoint

- 1 answer

In my custom APEX REST endpoint, I want to have the exact same pagination as the SFDC REST API. Is there an easier way to achieve this than implementing the logic from scratch? I would love to just extend abstract base class that holds all this logic and is used by the SFDC REST API as well. Or something similarly convenient, that doesn't involve me re-inventing the wheel.

Thanks in advance, Peter



Unfortunately, you won't find a "built-in" paging mechanism in the Apex REST framework.

However, basic paging can be achieved using the LIMIT and OFFSET clauses in your underlying SOQL query.

I'm assuming you're going to apply this to a custom GET request, and one of the drawbacks of this is the lack of a server-side cursor. So, in order to maintain your paging context, you would need to pass an updated offset value on each subsequent query, depending on which way the paging is going.

Also, I would highly consider applying a permanent ordering clause for the SOQL query, as well. Though you can mostly rely on getting the same results in the same order without one, it's not guaranteed to be consistent; better safe than sorry.