There’s a page on the web with the same title – Using Batch Apex. I tried to digest it in one sitting and it proved to be somewhat hard. So I went to Stack Overflow for help.
If you’re having the same problem, check out Dummy updates of Salesforce database records and execution governors. Nothing beats a simple working example. Just make sure you have an object called Acme_Portfolio__c with an Id field.
I’d like to elaborate on getting the most out it. Let’s say you just need to trigger updates. And you can use
Database.executeBatch(new batchClass());
to execute the batch and then check the results in Setup | Monitoring | Apex Jobs. What if you want to schedule it and get some kind of report on the executed (or failed) batches?
Turns our it’s pretty easy. Just wrap it in a class that implements Schedulable interface.
global class batchSchedule implements Schedulable
{
global void execute(SchedulableContext SC)
{
Database.executeBatch(new batchClass());
}
}
Now you can use the finish() function to add reporting functionality.
global void finish(Database.BatchableContext BC)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Update of Acme Portfolio ' + a.Status);
mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
The code is pretty much self explanatory and doesn’t require any changes to work. It pulls out all the stats on the executed batches, then gets an email of the user who created the class. Messaging.sendEmail() takes care of the rest.
Hope this helps someone gets a quick hold of Batch Apex.




