Simple Batch Apex example in Salesforce

Apex Code:

global class batchAccountUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         for(Account a : scope)
         {
             a.Name = a.Name + 'Updated';            
         }
         update scope;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Run the batch apex for testing it in Developer Console.


Output:


Cheers!!!

18 comments:

  1. Hi,

    This is kumar, I am getting an error for this code. Please help me.

    Error: Compile Error: Argument type of global method must also be global: LIST at line 9 column 17

    Like this i am getting to the error Please help me.

    Regards
    kumar

    ReplyDelete
  2. It is working fine for me.

    Copy the code and paste it and then execute it.

    Cheers!!!

    ReplyDelete
  3. Hi,

    Thanks for given to the reply, But i am facing to the same problem to copy and paste the above code.

    Regards
    kumar

    ReplyDelete
  4. Hey Magulan ,
    Can u help me how to write a Batch apex class to insert a record more 50million records with an example.
    Thanks in advance

    ReplyDelete
    Replies
    1. You cannot handle more than 50 million records.

      Check the below site

      http://www.infallibletechie.com/2012/05/batch-apex.html

      Cheers!!!

      Delete
  5. Please post test class for this batch class.

    ReplyDelete
  6. Hi
    Could you please suggest if want to revert the name as previous.then what I do?
    regards
    Sumit Shukla

    ReplyDelete
    Replies
    1. Create another batch with below code

      for(Account a : scope)
      {
      a.Name = a.Name.remove('Updated');
      }
      update scope;

      Cheers!!!

      Delete
  7. Thank you very much. Its working fine.

    ReplyDelete
  8. Hmm, i'm getting the following error: 'Method does not exist or incorrect signature: batchAccountUpdate()'

    ReplyDelete
  9. Thank you so much for your example on batch apex.....
    i am fresher, i need to know more about batch apex can pls give me some more links like this

    ReplyDelete
  10. Hi,
    Thank you so much. Please post test class for this batch class.

    ReplyDelete
  11. global class AccountBatch_AC implements Database.Batchable{
    global Database.QueryLocator start(Database.BatchableContext bc){
    String strQuery='SELECT id,name FROM Account';
    return Database.getQueryLocator(strQuery);
    }
    global void execute(Database.BatchableContext bc,List lstAccObj){
    for(Account accObj:lstAccObj){
    accObj.name='Mr'+accObj.name;
    lstAccObj.add(accObj);
    }
    update lstAccObj;
    }
    global void finish(Database.BatchableContext bc){}

    ReplyDelete
    Replies
    1. [11/6/2015, 1:17 PM] +91 81973 15768: error idi vastundiCannot modify a collection while it is being iterated.

      Delete
  12. can i call metadata in batchclass,i had used metadata to create customfields in custom object based on Standard object.I created a Apex class to create custom fields.

    ReplyDelete
    Replies
    1. Yes. You can do this. But check Salesforce documents for Apex Class version supported.

      Delete