0

How to get all record in batch? my requirement need to get all records from a object. but, SFDC batch size limit is 2000, even if i use iterable batch still can not get all records. If i use Database.Stateful(), it will trigger to "instacne too big" or "string to big". For example, my object records more 30K, i need to get all and compare all record base on some condition. Searching google, looks like can not get all record. So, would you mind give me a solution for me?

  • The batch job reads all the records, in execution batches of 2000 per execution of the "execute" method. Why do you think you need to have all the records available at once? – Dominic Nov 3 '14 at 9:39
  • I need to compare all result and de-duplicate(meger) record. if 2000 per execution, I still can not get all. – Tao Yu Nov 3 '14 at 11:32
  • You dont need to compare the whole set at once. Take each batch and compare against the db. – Dominic Nov 3 '14 at 14:58
2

In the start() method you can query all the records of your object (up to 50 million providing you use the QueryLocator rather than an Iterable). The 2000 limit is the number of objects sent per batch. For example, if your start() method returns 50 000 objects, you will have 25 batches (25 * 2000).

So you can do like this :

global class MyBatchClass implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
      String query = 'select id from Account'; // Example to get all your Accounts

      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
       ...
    }

   global void finish(Database.BatchableContext BC){
       ...
    }

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

  • Don't you mean 25 batches? – Keith C Nov 3 '14 at 10:15

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.