Learn Apex Batch Job

Batch Class in Salesforce


Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be pro grammatically invoked at run time using Apex.

Need of Batch Apex: - As you all might know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.
We have to create an global apex class which extends Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs
Example:
Class implementingBatchJob implements Database.Batchable{

global final String Query;
global deleteOpp (String q)
{
Query=q;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List scope)
{
List <Opp> lstOpp = new list<Opp>();
for(Sobject s : scope)
{
 Oppa = (Opp)s;
lstOpp .add(a);
}
Delete lstOpp ;
}
global void finish(Database.BatchableContext BC)
{
  //Send an email to the User after your batch completes
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {‘sforcesfdc@gmail.com’};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done‘);
mail.setPlainTextBody('The batch Apex job processed ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

//This is how the batch class is called.
id batchinstanceid = database.executeBatch(new implementingBatchJob (‘select Id from opp’))
 Advantages of using Batch Apex?



  • Higher Governor Limits
  • Can be used to process in batches
  • Can be scheduled to run at different time. 




  • Work around to other governor limits e.g. Send More than 10 E-mails

  • Governor limit difference in using batch
    Area
    Normal Context
    Batch Context
    SOQL queries100 SOQL per cycle200 SOQL per cycle
    records retrieved by SOQL queries50,00050,000,000 (getQueryLocator)



    Heap size6 MB12 MB

    Batch Apex Governor Limits


    • Up to five queued or active batch jobs 
    •  User can have up to 50 query cursors open at a time. 
    • A maximum of 50 million records can be returned in the Database.QueryLocator object. 
    • If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200
    • startexecute, and finish methods can implement up to 100 callouts
    • Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn’t cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running

    You could monitor the class by following below steps:
    To monitor or stop the execution of the batch Apex Batch job, go to Setup->Monitoring->Apex Jobs or Jobs-> Apex Jobs.

    Start:

    Syntax:
    global (Database.QueryLocator | Iterable) start(Database.BatchableContext bc) {}

    Execute:

    Syntax:
    global void execute(Database.BatchableContext BC, list<sobject<){}
    where , list<sObject< is returned by the Database.QueryLocator method.

    Finish:

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

    Scheduling a batch apex class with in that batch class?



    global class AccountUpdateBatchJob 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 by Batch job';
            }
            update scope;
        }
        global void finish(Database.BatchableContext BC) {
            OtherBatchJob obj = new OtherBatchJob();
            DataBase.executeBatch(obj);
        }

    }





    Calling on Batch Apex

                AccountJob obj = new AccountJob ();
                DataBase.executeBatch(obj);

    Scheduler Class For Batch Apex


    global class AccountUpdateBatchJobscheduled implements Schedulable 
    {
        global void execute(SchedulableContext sc) 
        {
            
    AccountJob b = new AccountJob (); 
            database.executebatch(b);
        }
    }
    How to Schedule scheduler class

    There are two option we have schedule the scheduler classes.
    1) By System Scheduler.
    2) By Developer console

    By Developer console 

    Execute below code from developer console :-

    AccountUpdateBatchJobscheduled m = new AccountUpdateBatchJobscheduled();
    String sch = '20 30 8 10 2 ?';
    String jobID = system.schedule('Merge Job', sch, m);



    What is Database.State full interface?
    To maintain variable value inside the Batch class, Database.Stateful is used.

    What is Database.AllowCallouts?

    To use a callout in batch Apex, you must specify Database.AllowsCallouts in the class definition. For example:
    global class SearchAndReplace implements Database.Batchable<sObject>,
       Database.AllowsCallouts{
                  //Business logic you want by implementing Batchable interface methods
    }
    Callouts include HTTP requests as well as methods defined with the webService keyword.

    How to use Database.Stateful in batch Apex?





    we want to add up the sum of all opportunities for each Account and store it in a field on Account called test__c 
    So we build a batch that receives all opportunities withn an Account and Amount, and keeps a running total for each account using an account map, and finally update the accounts in the final part of the batch:

    global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

       global final String query;
       global Map<Id, Account> accountmap;
      
       global SummarizeAccountTotal(){
       accountmap = new Map<Id, Account> ();
       }

       global Database.QueryLocator start(Database.BatchableContext BC){
          return Database.getQueryLocator(query);
       }
       
       global void execute(Database.BatchableContext BC, List<sObject> scope){
       
       List<Opportunity> ops = (List<Opportunity>)scope;
       
       for (Opportunity o : ops) {
       
       if (accountmap.containskey(o.AccountId)) {
       Account a = accountmap.get(o.AccountId);
       a.test__c += o.Amount;
       accountmap.put(o.AccountId, a);
       }
       else{
       accountmap.put(o.AccountId, new Account (Id = o.AccountId, test__c = o.Amount));
       } 
             
       }
       }
       

    global void finish(Database.BatchableContext BC){
       
       try {
       update accountmap.values();
       }
       catch (Exception Ex) {
       system.debug(Ex);
       }
       
       }
    }

    Using Batch Apex on millions of records



    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 .

    Maximum number of batch Apex jobs in the Apex flex queue that are inHolding status100
    Maximum number of batch Apex jobs queued or active concurrently35
    Maximum number of batch Apex job start method concurrent executions41
    Maximum number of batch jobs that can be submitted in a running test5
    Maximum number of query cursors open concurrently per user for the Batch Apex start method15
    Maximum number of query cursors open concurrently per user for the Batch Apex execute and finish methods5
    For loop list batch size200
    Maximum number of records returned for a Batch Apex query inDatabase.QueryLocator50 million

    Comments

    Popular posts from this blog

    All about workflow rule , limitation and important points with example

    Custom Setting in Salesforce

    Action tags in VisualForce Salesforce