Skip to main content

Batch APex Interview Questions

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.

Why use Batch Apex?
A Batch class allows you to define a single job that can be broken up into manageable chunks that will be processed separately.
- One example is if you need to make a field update to every Account in your organization. If you have 10,001 Account records in your org, this is impossible without some way of breaking it up. So in the start() method, you define the query you’re going to use in this batch context: ‘select Id from Account’. Then the execute() method runs, but only receives a relatively short list of records (default 200). Within the execute(), everything runs in its own transactional context, which means almost all of the governor limits only apply to that block. Thus each time execute() is run, you are allowed 150 queries and 50,000 DML rows and so on. When that execute() is complete, a new one is instantiated with the next group of 200 Accounts, with a brand new set of governor limits. Finally the finish() method wraps up any loose ends as necessary, like sending a status email.
- So your batch that runs against 10,000 Accounts will actually be run in 50 separate execute() transactions, each of which only has to deal with 200 Accounts. Governor limits still apply, but only to each transaction, along with a separate set of limits for the batch as a whole.

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.

Comments

Popular posts from this blog

Define & View Custom Big Object

Define a Custom Big Object: You can define custom big objects with Metadata API or in Setup. After you define and deploy a big object, you can view it or add fields in Setup. After you’ve deployed a big object, you can’t edit or delete the index. To change the index, start over with a new big object. To define a big object in Setup, see Salesforce Help. View a Custom Big Object in Setup After you’ve deployed your custom big object, you can view it by logging in to your organization and, from Setup, entering  Big Objects  in the  Quick Find  box, then selecting  Big Objects . Click the name of a big object, to see its fields and relationships. View a Custom Big Object in Setup After you’ve deployed your custom big object, you can view it by logging in to your organization and, from Setup, entering  Big Objects  in the  Quick Find  box, then selecting  Big Objects . Click the name of a big object, to see its fields and ...

ASYNC SOQL - with Big Objects

Async SOQL Async SOQL is a method for running SOQL queries when you can’t wait for immediate results. These queries are run in the background over Salesforce big object data. Async SOQL provides a convenient way to query large amounts of data stored in Salesforce. Async SOQL is implemented as a RESTful API that enables you to run queries in the familiar syntax of SOQL. Because of its asynchronous operation, you can subset, join, and create more complex queries and not be subject to timeout limits. This situation is ideal when you have millions or billions of records and need more performant processing than is possible using synchronous SOQL. The results of each query are deposited into an object you specify, which can be a standard object, custom object, or big object. The limit for Async SOQL queries is one concurrent query at a time. Async SOQL Versus SOQL SOQL and Async SOQL provide many of the same capabilities. So when would you use an Async SOQL query instead of s...

Publish Paltform Events

Publishing Platform Events The Salesforce enterprise messaging platform offers the benefits of event-driven software architectures. Platform events are the event messages (or notifications) that your apps send and receive to take further action. Platform events simplify the process of communicating changes and responding to them without writing complex logic.  Publishers and subscribers communicate with each other through events.  One or more subscribers can listen to the same event and carry out actions. Here we will see how many ways you can able to publish the platform events in Salesforce.  After a platform event has been defined in your Salesforce org, publish event messages from a Salesforce app using processes, flows, or Apex or an external app using Salesforce APIs.Here is the simple platform event object we will be using in the examples here Here are the different ways you can able to publish the platform events in Salesforce Option 1: Using Proces...