15

Part of our app behaviour is going over millions on records and updating their data.

From what I read, we can do it using Batch Apex although I am not certain I fully understand how it works.

Here is a code snippet of our Batchable class:

       global class EnrichContacts implements Database.Batchable<sObject>
       {
            global final String Query;

            global EnrichContacts(String q)
            {
               Query=q;
            }

            global Database.QueryLocator start(Database.BatchableContext BC)
            {
               return Database.getQueryLocator(query);
            }

            global void execute(Database.BatchableContext BC,List<sObject> scope)
            {
                  List <Contact> modifiedContacts = new list<Contact>();
                  for(Sobject s : scope)
                  {
                      Contact a = (Contact)s;
                      // Add some information to the contact
                      modifiedContacts.add(a);
                  }
                  update modifiedContacts;
            }

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

       //Calling the Batch
       id bId = database.executeBatch(new EnrichContacts(‘select Id from Contact’))

My Concerns:

  • Will this code works on millions of contacts?
  • Does Force.com runtime calls the "execute" method each time with 50k contacts?
  • Does the execute method has a time-out after X seconds?
  • Can we control the amount of records being delivered to the "execute" method? (for example if we wish to deliver each time 500 contacts)

Appreciate any ideas...

22

Answers to your concerns :

1)Yes batch apex are asynchronous process and can support upto 50 million records and hence for 1 million this code will run

2)You will specify batch size while you invoke batch .Say i specify batch size as 400.Then 50000/400 number of batches will run.In each batch context 400 records will picked .

3)No there are no time outs as such till Summer 13 .There are heap size limits and script statements that you would need to be careful .From next release there is no script statements limit but there is some time out limit for synchronous calls and 60000 milliseconds of CPU time for async transactions.

4)Yes we can control this through the scheduler script by specifying the batch size

String query = 'select Id from Contact'
    EnrichContacts batchpr = new EnrichContacts (query);
    Id batchprocessId = Database.executeBatch(batchpr,500);

The above script that you have i have split and 500 is my batch size .

5
  • 1
    See page 230 of the Winter '14 release notes for the new limits. Async transactions must finish within 60,000 milliseconds - help.salesforce.com/help/doc/en/… – Mike Chale Aug 27 '13 at 13:35
  • @MikeChale Thanks for update.I added that too in my answer. – Mohith Shrivastava Aug 27 '13 at 13:49
  • 1
    @MikeChale the limit is 60,000ms of CPU time, which is markedly different than clock-on-the-wall time. Time spent on database operations is, as one example, not included against this limit. – ca_peterson Aug 27 '13 at 17:33
  • 1
    Thank you for the clarification, @ca_peterson. I missed that (rather important) distinction. – Mike Chale Aug 27 '13 at 17:38
  • 2
    CPU time is not your transaction time at any point. This is time that is required to execute the code line as that is done by the CPU. The time required for the database statements is not included in the CPU time. So for async calls also the transaction time might be more than the 60000 ms but the time required to execute other code lines apart from database statements in aggregate should not exceed this value. – Mukul Goel Aug 27 '13 at 19:44

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.