Quantcast
Monday , December 10 2018
Home / Salesforce Interview Questions / Batch class : Salesforce Interview Question

Batch class : Salesforce Interview Question

Salesforce Interview Question on Batch class

 

What is Batch Apex?

It is a class which is capable of accepting records in bulk and then depending upon the batch size the group of records are broken into Batches and are processed asynchronously.

 

When should we use a batch class?

We should batch apex when we are dealing with records in bulk mainly the record size above 10000 and cannot be handled by the normal DML operations.

 

What is the basic structure of a Batch class?

the basic structure of a batch consists of three methods:

  1. Start
  2. Execute
  3. Finish

 

Describe each method of a batch class?

Start method:

Syntax: global Database.QueryLocator start(Database.BatchableContext BC)

This method receives the batchable context variable as an argument and returns a query locator variable.

 

Execute method:

Syntax:  global void execute(Database.BatchableContext BC, List<Account> scope)

This method receives the batchable context variable and an additional argument as scope which of list of subject type we are working with.

 

Finish method:

Syntax: global void finish(Database.BatchableContext BC)

This method receives the batchable context variable and is the last method which gets executed after all the batches have been processed, we can perform all the post execute logic in this method like sending an email etc.

 

What is a scheduler class?

A scheduler class implements the Schedulable interface and have to implement its execute method.

 

how to schedule a batch class ?

there are two ways to schedule a batch class:-

  1. Using developer console
  2. Using System Scheduler

By Developer console we have to pass a cron variable like below:-

AScheduleClass A = new AScheduleClass();

String schedule =  ’20 30 8 10 2 ?’; // cron object

String jobID = system.schedule(‘Merge Job’, schedule, A);

 

Via System Scheduler:

 

  1. Go to setup and in quick search bar type schedule. Then click on schedule apex.
  2. Select schedule created and set the time with help of the Salesforce UI.

 

Can we call a batch class from another batch class?

Yes, we can call one batch class from another batch class but only if it is called from its finish method.

 

Can we call a Future method from batch class?

no, we cannot directly call a future method from a batch class.

 

What is Database.Stateful used for in batch class?

As batch class runs asynchronously, to maintain the state of a variable across each batch we implement Database.Stateful.

 

How many can concurrent batch jobs be added to the queue?

At most, there can be 5 bath jobs queued at a time.

 

How can we track the status of the current running batch job?

The job Id returned by the batchable context variable helps us in finding the status of a batch through AsyncApexJob.Status.

 

Q . What is the purpose of start method in Batch Class?

Start object returns the list or group of records on which we are going to make changes.

 

What is database.getQuerylocator and what are its advantages?

It is used to fetch records from the database, if used in batch class it can fetch up to 50 millions records.

 

******* More questions to come

Want to learn more about batch Classes and Asynchronous Apex have a look at link below

Asynchronous Apex

About Sumit Datta

Sumit Datta
I am a 3x Certified Salesforce developer with overall 5 years of IT experience and 3 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Check Also

MODAL Box

How to create a Modal box in Lightning Component?

How to create a Modal box in Lightning Component? In this post we are going …

Salesforce Lightning Training

How to send parameters in AuraEnabled methods in Salesforce Lightning Components?

How to send parameters in AuraEnabled methods in Salesforce Lightning Components? Passing value to and …

Salesforce Lightning Tutorials

How to use lightning accordion

How to use lightning accordion and how to dynamically add accordion section in lightning. In …

Salesforce Lightning Tutorial

How to fetch and display list of records in Lightning Component

How to fetch and display list of records in Lightning Component? In this post we …