function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nishad BashaNishad Basha 

How to write the asynchronous Batch Apex in salesforce?

can you please give me the example programm of asynchronous Batch Apex. please give some ideas.
Sagar PareekSagar Pareek
http://salesforcesource.blogspot.in/2010/02/utilizing-power-of-batch-apex-and-async.html
anand k 11anand k 11
Hi Nishad,

You can refer below link for asynchronous batch apex in salesforce

https://resources.docs.salesforce.com/194/latest/en-us/sfdc/pdf/salesforce_async_processing.pdf

Thanks
Anand
Nishad BashaNishad Basha
can you please give me the example code of above scenario.
anand k 11anand k 11
Hi Nishad,

You can find the sample code 
https://developer.salesforce.com/blogs/developer-relations/2013/01/apex-template-asynchronous-apex.html

Thanks
Anand
MagulanDuraipandianMagulanDuraipandian
Check this - http://www.infallibletechie.com/2015/01/simple-batch-class-for-birthday.html
Nishad BashaNishad Basha

Magulan D
Can you give me the more  examples of asynchronous Batch Apex jobs in salesforce
Amit Chaudhary 8Amit Chaudhary 8
Please download below book for async job in salesforce
https://resources.docs.salesforce.com/194/latest/en-us/sfdc/pdf/salesforce_async_processing.pdf

Apex batch job is itself a asynch job. Please check below post with example
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
http://www.oyecode.com/2011/10/how-to-use-batch-apex-in-salesforce.html
global class batchOpportunityUpdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name FROM Opportunity';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
         for(Opportunity a : scope)
         {
             a.Name = a.Name + 'Updated';            
         }
         update scope;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

Disadvantages of batch processing:

--> It runs asynchronously, which can make it hard to troubleshoot without some coded debugging, logging, and persistent stateful reporting. It also means that it's queued to run, which may cause delays in starting.
--> There's a limit of 5 batches in play at any time, which makes it tricky to start batches from triggers unless you are checking limits.
--> If you need access within execute() to some large part of the full dataset being iterated, this is not available. Each execution only has access to whatever is passed to it, although you can persist class variables by implementing Database.stateful.
--> There is still a (fairly large) limit on total Heap size for the entire batch run, which means that some very complex logic may run over, and need to be broken into separate batches.

Please let us know if this will help you.

Thanks,
Amit Chaudhary
Rafa GoldaracenaRafa Goldaracena
global class LeadProcessor  implements 
      Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
        return Database.getQueryLocator(
                        'SELECT ID FROM Lead '  );       
    }
    global void execute(Database.BatchableContext bc, List<lead> records){
        // process each batch of records
           List<Lead> upleads = new List<Lead>();       
        for (Lead lead : records) {           
                Lead.LeadSource = 'Dreamforce' ;
                // add contact to list to be updated
                upLeads.add(lead);       
        }
        update  upLeads;
    }    
    global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }    

}

If this helps you.....