Conquer Large Datasets with Apex Cursors (Beta)

Struggling to process massive datasets in Salesforce with Apex? Buckle up, because Apex Cursors (currently in Beta) are here to save the day!

What are Apex Cursors?

Imagine a fire hose of data spewing out a SOQL query.  Normally, Apex would try to hold the entire stream in memory, which can be overwhelming for both your code and Salesforce's resources.  Apex Cursors act like a bucket, allowing you to process the data in manageable chunks.

Benefits of Apex Cursors

  • Handle Large Datasets: Don't be intimidated by millions of records. Cursors break them down into bite-sized pieces for efficient processing.
  • Flexibility: Need to pause and come back later? No problem! Cursors allow you to navigate forward and backward through the results.
  • Queueable Power: Unlike Batch Apex, Cursors integrate seamlessly with queueable jobs, making them ideal for high-volume, high-resource tasks.

How Cursors Work

  • Stateless Processing: Each chunk of data is processed independently, keeping memory usage low.
  • Cursor.fetch() Method: This method controls how much data you retrieve at a time. Specify the starting position (offset) and number of records to fetch.
  • Tracking Your Place: Since cursors are stateless, it's up to you to keep track of your progress within the result set.

Cursor Mechanics

  • Creating a Cursor: Use Database.getCursor() or Database.getCursorWithBinds() on your SOQL query.
  • Fetching Data: The Cursor.fetch() method retrieves the requested data chunk.
  • Cursor Limits: There's a maximum of 50 million rows per cursor, and 10 fetch calls allowed per transaction (both synchronous and asynchronous).
  • New Exceptions: Apex introduces System.FatalCursorException for unrecoverable errors and System.TransientCursorException for retryable issues.
  • Cursor Expiration: Cursors have the same expiration limits as API Query cursors.

Governor Limits

  • Limits Class Methods: Use Limits.getApexCursorRows() and Limits.getLimitApexCursorRows() to check the maximum number of rows allowed per cursor.
  • Fetch Calls and Daily Limits: Similar methods (Limits.getFetchCallsOnApexCursor() and corresponding limits) exist for fetch calls and daily cursor usage.

Caution

Apex Cursors are currently in Beta. So, experiment responsibly.

Comments