function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gopikrishna DasariGopikrishna Dasari 

I want to process more than 50 million records in batch apex how can we do please help out me on this

Best Answer chosen by Gopikrishna Dasari
SwethaSwetha (Salesforce Developers) 
HI Gopi,

As per documentation , "A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed."

What is your usecase of having to process 50 million records ? You should make sure your queries are tuned properly to run as quickly and efficiently as possible.

I would recommend working on the SOQL query to make it more selective so that it returns less than 50 million records for you to be able to use in a batch apex.

Please see the below articles for tuning your SOQL query.

Working With Very Large SOQL Queries
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm

Make SOQL Query Selective
https://help.salesforce.com/s/articleView?id=000325257&type=1

Also, if processing the records synchronously is not a strict requirement, consider using asynchronous processing options like Queueable Apex or Future Methods. These allow you to offload processing to separate execution contexts, providing more flexibility in handling large data volumes.

You can also consider using Bulk API(PK  Chunking) https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/async_api_headers_enable_pk_chunking.htm

If this information helps, please mark the answer as best. Thank you

All Answers

Arun Kumar 1141Arun Kumar 1141

Due to a platform governor limit, it is not possible to retrieve more than 50 million records in batch apex. However if your query exceeds this limit, it will likely result in a limit exception.

To work around this limitation, you can use Batch chaining , that will allow you to have upto 5 batch job chained . For Example , First batch can proccess upto 45 million records and rest of the records can be send to another batch and than 2 batch can proccess further records.You may need to develop custom logic to filter the records that are being proccessed.

SwethaSwetha (Salesforce Developers) 
HI Gopi,

As per documentation , "A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed."

What is your usecase of having to process 50 million records ? You should make sure your queries are tuned properly to run as quickly and efficiently as possible.

I would recommend working on the SOQL query to make it more selective so that it returns less than 50 million records for you to be able to use in a batch apex.

Please see the below articles for tuning your SOQL query.

Working With Very Large SOQL Queries
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm

Make SOQL Query Selective
https://help.salesforce.com/s/articleView?id=000325257&type=1

Also, if processing the records synchronously is not a strict requirement, consider using asynchronous processing options like Queueable Apex or Future Methods. These allow you to offload processing to separate execution contexts, providing more flexibility in handling large data volumes.

You can also consider using Bulk API(PK  Chunking) https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/async_api_headers_enable_pk_chunking.htm

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Gopikrishna DasariGopikrishna Dasari
Hi Swetha,
Thank you for your ansewr.