Use Apex Cursors for Expanded SOQL Query Result Support (Generally Available)

Use Apex cursors and pagination cursors to work with large SOQL query result sets in manageable parts. This feature, now generally available, includes enhancements since the beta release. Standard cursors, when combined with queueable Apex, address the limitations of batch Apex. Pagination cursors are designed for UI elements, such as multipage record lists. Both Apex cursor types can traverse forwards and backwards through result sets and can handle high-volume, high-resource processing.

Where: This feature is available in API version 66.0 and later. The feature is available in Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.

Why: Apex standard cursors offer an efficient alternative to batch Apex for processing large result sets. If you use an Apex batch job, you must set the batch size to the smallest value that stays within platform limits. The batch size is the same across the entire result set, so processing that result set can require an increased number of transactions. In contrast, using an Apex cursor with a chain of queueable jobs gives you flexibility in how many records are processed with each transaction. This approach provides better utilization of the asynchronous Apex limit. Calling Cursor.fetch() still counts against the SOQL query limit, and the rows fetched count against the SOQL query row limit.

How: This feature, now generally available, includes these enhancements.

  • A PaginationCursor class to implement pagination in a UI. Calling the PaginationCursor.fetchPage() and PaginationCursor.fetchDeleted() methods count against the SOQL query limit, and the rows fetched count against the SOQL query row limit.
  • Debug log events to track cursor operations.
  • Updated cursor governance limits. The maximum cumulative number of new cursor rows per 24-hour period is 100 million.
  • These new methods to track usage against new governor limits.
    • Limits.getApexCursors() and its upper bound Limits.getLimitApexCursors() method
    • Limits.getApexPaginationCursors() and its upper bound Limits.getLimitApexPaginationCursors() method
    • Limits.getApexPaginationCursorRows() and its upper bound Limits.getLimitApexPaginationCursorRows() method
  • Cursor and pagination cursor support in AuraEnabled serialization and deserialization. You can serialize a cursor in an @AuraEnabled response and deserialize a cursor as an input parameter in an @AuraEnabled method.