Wednesday, September 12, 2018

Batch class in salesforce

As platform limit plays a very important role in Salesforce, it becomes more important to stay within the platform limits when we are dealing with large number of records. With batch apex we can process large number of records asynchronously.

Understanding batch apex with an example would be easier, Let’s try to understand it with a scenario described below.

Let’s say you want to process 1 million records using Batch Apex. The execution logic of the batch class is called once for each batch of records you are processing. Each time you invoke a batch class, the job is placed on the Apex job queue  and is executed as a discrete transaction. Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits. If one batch fails to process successfully, all other successful batch transactions aren’t rolled back.

When we use batch apex, we implement Database.batchable() interface.

The Database.batchable() interface contains three methods that we need to define inside batch class, and they are as below:

1)start

2)execute

3)Finish

All the methods in the Database.Batchable interface require a reference to a Database.BatchableContext object.

Syntax of batch class:

global class batch implements Database.Batchable < sObject > {

global (Database.QueryLocator or  Iterable<sObject>)

start(Database.BatchableContext bc) {

//query on object;

//return Database.getQueryLocator(query);

}

global void execute(Database.batchableContext bc, List < SObject > scope) {

//some processing.

}

global void finish(Database.BatchableContext bc) {

//job such as sending email or calling another batch class

}

}

Start method collects the data for processing. This method is called once at the beginning of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job.

Most of the time a QueryLocator does the trick with a simple SOQL query to generate the scope of objects in the batch job but if you need to do something crazy like loop through the results of an API call or pre-process records before being passed to the execute method, you might need to go with the Custom Iterators.

With the QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed and you can query up to 50 million records. However, with an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.

Execute method performs operations. The default batch size is 200 records. Batches of records are not guaranteed to execute in the order they are received from the start method.

This method takes the following:

A reference to the Database.BatchableContext object.

A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, use the returned list.

Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each. The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction aren’t rolled back.

Finish method generally used for sending emails or calling another batch class when the current batch is completed. Using Database.BatchableContext object we can track the progress of the batch job in finish method as shown below.

getJobID is the instance method with the Database.BatchableContext object.

It returns the ID of the AsyncApexJob object associated with this batch job as a string.

public void finish(Database.BatchableContext BC){

   AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,

      TotalJobItems, CreatedBy.Email

      FROM AsyncApexJob WHERE Id =

      :BC.getJobId()];

   // Send an email to the Apex job's submitter notifying of job completion.

   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

   String[] toAddresses = new String[] {a.CreatedBy.Email};

   mail.setToAddresses(toAddresses);

   mail.setSubject('Batch Job Status ' + a.Status);

   mail.setPlainTextBody

   ('The batch Apex job processed ' + a.TotalJobItems +

   ' batches with '+ a.NumberOfErrors + ' failures.');

   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

Invoking a Batch Class:

To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:

SampleBatchClass batchObject = new SampleBatchClass ();

Id batchId = Database.executeBatch(batchObject);

You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch. The optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records.

Id batchId = Database.executeBatch(batchObject, 100);

The Database.executeBatch method returns the ID of the AsyncApexJob object, which you can use to track the progress of the job.

AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors

                    FROM AsyncApexJob WHERE ID =: batchId];

Scheduling apex job:

To Schedule the batch class, you need a class which implements schedulable interface,

global class schedulebatch implements schedulable{

   global void execute(Schedulablecontext sc)

          {

             updateNameOfAccount acc=new updateNameOfAccount();

             //Database.executeBatch(instance,size);

             Database.executeBatch(acc,200);

          }

}

Now, you can schedule class schedulebatch from User Interface,

Setup>develop>apex classes> schedule apex.

Looking to Master Batch Apex Essential? Have a look at the course specially designed to Master Batch Apex.


Throughout the course, we'll delve into crucial subjects such as understanding Batch Apex's role, controlling batch size, harnessing the power of Database.Stateful, executing callouts, implementing best practices, exploring limitations, triggering batch classes from apex triggers, mastering future methods, and comprehending queueable Apex. Additionally, we'll discuss advanced concepts like chaining Batch Jobs and dissect real-world scenarios where Batch Apex proves invaluable.

Here's a glimpse of the comprehensive table of contents:

        1) What is Batch Apex in Salesforce?
        2) How to control number of batches in Batch Apex using scope size?
        3) What is Database.Stateful in Batch Apex in Salesforce?
        4) How to do callouts from Batch Apex in Salesforce?
        5) What are Salesforce batch apex best practices?
        6) What are Salesforce batch apex limitations?
        7) How to call batch apex class from apex trigger in Salesforce? 
        8) What is future method in Salesforce?
        9) What is Queueable apex in Salesforce?
      10) Explain chaining of Batch Jobs?
      11) Explain the error "Too many queueable jobs added to the queue"?
      12) Explain the error "Maximum stack depth has been reached"?
      13) Real time scenario for use of Batch Apex?
    

To further enhance your preparation, we've included a dedicated section with 30 interview questions, ensuring you're well-equipped to tackle any interview related to Batch Apex.

Get ready to embark on a learning journey that will sharpen your Batch Apex skills and open new doors of opportunity. Enroll now and let's delve into the world of Batch Apex together!

10 comments: