Batch Apex #inSalesforce

Batch Apex #inSalesforce


Scenario: Activate and update the Account's description 


Create an Apex Batch Class:

The Database.Batchable interface contains three methods that must be implemented - Start, Execute, and Finish


global class batchJobAccounts implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id, Active__c, Description FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Account> scope){
        for(Account act: scope){
            act.Active__c = True;
            act.Description = 'Activated';
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc){
        system.debug('!!!!Job Completed!!!!!');
    }
}


To monitor the job : Setup | Apex Jobs



To debug:

Go to Developer Console | Anonymous Window |
batchJobAccounts batchJob = new batchJobAccounts();
Database.executeBatch(batchJob,200);



Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Comments