I have almost 1cr records which needs to be updated . i used below batch class. But as per the limit of 10000

Total number of records retrieved by Database.getQueryLocator 10,000

how can i process for 1cr records then.is their any way.any help is appreciated.

here is my batch class

public class ContactDeletesa implements database.Batchable<sobject>{

   public database.QueryLocator start(database.BatchableContext BC){
       String Query='select id,level__C from contact where level__C in (\'1\',\'2\',\'3\')';
       return database.getQueryLocator(Query);

   public void execute(database.BatchableContext BC,List<contact>scope){
        List<contact>conList=new list<contact>();
        Map<string,string> lavelMap = new Map<string,string>{'1' => 'first', '2' => 'second', '3' => 'third','4' => 'fourth'};

      for(contact co:scope){
    catch( exception e){  

  public void finish(database.BatchableContext BC){


  • Is there any way if i want to process the 10000 records in one batch class and repeat the batch
    – sandy
    Dec 14 '17 at 9:12

The query locator in the Start method can query upto 50 million no of rows and hence you should be able to process any number of records within the limit of 50 million.

The execute method will take number of rows equivalent to scope defined by the batch process and salesforce will chunk records as described in the docs

Conclusion is you should be able to process upto 5 crores without any issues .One thing to consider is to optimize query using SOQL Optimizer

  • i am not able to understand what is the query optimize and how to do that .please suggest
    – sandy
    Dec 14 '17 at 8:41

Maximum number of records returned for a Batch Apex query in Database.QueryLocator 50 million So u can create 2 batch classes call the 2nd batch class at the end of 1st batch class finish method and use offset to skip first 50 million records in second batch query

  • offset works only for 2000 records...
    – kurunve
    Dec 14 '17 at 16:02

I hope this would give you better Understanding about Query Optimizer and Query Plan Tool.

Force.com Batch Apex and Large Data Volumes: https://developer.salesforce.com/blogs/engineering/2013/02/force-com-batch-apex-and-large-data-volumes.html

Force.com Query Optimizer FAQ: https://help.salesforce.com/articleView?id=000181277&type=1

Query Plan Tool: https://help.salesforce.com/articleView?id=000199003&type=1

Make SOQL query selective: https://help.salesforce.com/articleView?id=How-to-make-my-SOQL-query-selective&type=1

Query & Search Optimization Cheat Sheet: http://resources.docs.salesforce.com/194/0/en-us/sfdc/pdf/salesforce_query_search_optimization_developer_cheatsheet.pdf

  • but my question is if my query fetching 1cr records then there is chances of timeout .How can i avoid is there any queueble/enque somthing like that i can use
    – sandy
    Dec 14 '17 at 9:53
  • You can have 200Records Per Batch schedule and Execute it. apexcoder.com/2016/05/19/… Dec 14 '17 at 10:53
  • but in start method the query will try to fetch 1cr records and there may be chances of timeout .how to tackle this
    – sandy
    Dec 14 '17 at 10:55
  • As per My Knowledge You will not be affected,Because query timeout in the batch Apex start method has been increased to 10 minutes. Dec 14 '17 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.