Sei sulla pagina 1di 8

Batch Apex

1. What are governing limits in salesforce?


A. As we know, Apex runs in multi-tenant environment; i.e. a single resource is shared by all the
customers and organizations. So, it is necessary to make sure that no one monopolizes the resources and
hence Salesforce.com has created the set of limits which governs and limits the code execution.
Whenever any of the governor limits are crossed, it will throw error and will halt the execution of
program.
From a Developer's perspective, it is of at most important to ensure that our code should be scalable and
should not hit the limits. All these limits are applied on per transaction basis.

2. Name some governing limits in salesforce?


Description
Total number of SOQL queries issued1 (This limit doesnt apply to
custom metadata types. In a single Apex transaction, custom
metadata records can have unlimited SOQL queries.)
Total number of records retrieved by SOQL queries
Total number of records retrieved by Database.getQueryLocator
Total number of SOSL queries issued
Total number of records retrieved by a single SOSL query
Total number of DML statements issued2
Total number of records processed as a result of DML
statements, Approval.process, or database.emptyRecycleBin
Total stack depth for any Apex invocation that recursively fires
triggers due to insert,update, or delete statements3
Total number of callouts (HTTP requests or Web services calls) in
a transaction
Maximum timeout for all callouts (HTTP requests or Web services
calls) in a transaction
Maximum number of methods with the future annotation allowed
per Apex invocation
Maximum number of Apex jobs added to the queue
with System.enqueueJob
Total number of sendEmail methods allowed
Total heap size4
Maximum CPU time on the Salesforce servers5
Maximum execution time for each Apex transaction
Maximum number of push notification method calls allowed per
Apex transaction
Maximum number of push notifications that can be sent in each
push notification method call

Synchronous
Limit
100

Asynchronous
Limit
200

50,000
10,000
20
2,000
150
10,000
16
100
120 seconds
50
50
10
6 MB
10,000
milliseconds
10 minutes
10
2,000

12 MB
60,000
milliseconds

3. What is Batch Apex?


Batch Apex is asynchronous execution of Apex code, specially designed for processing the large
number of records and has greater flexibility in governor limits than the synchronous code.
When to use Batch Apex?
When you want to process large number of records on daily basis or even on specific time of
interval then you could go for Batch Apex.
Also, when you want an operation to be asynchronous then you could implement the Batch
Apex. Batch Apex is exposed as an interface that must be implemented by the developer. Batch
jobs can be programmatically invoked at runtime using Apex. Batch Apex operates over small
batches of records, covering your entire record set and breaking the processing down to
manageable chunks of data.
We can call the apex code by creating object for the class (or) if the variables or methods are
static then we can call with class name.
Apex Code in the trigger will execute automatically for the DML operations.
If you want to execute apex code on a specific time then we should write batch class.
With Batch Apex we can process maximum of 50 million records.
Batch Apex is asynchronous (execute in future context).
While writing the batch class we should inherit Database.Batchable interface.
Database is a built in global class which contains inner global interface.
4.How many DML statements can be written in a transaction?
A. Total number of DML statements issued 150
5.What is the governing limit of SOQL query?
Description
Total number of SOQL queries issued (This limit
doesn't apply to custom metadata types. In a single
Apex transaction, custom metadata records can have
unlimited SOQL queries.)
Total number of records retrieved by SOQL queries

Synchronous
Limit
100

Asynchronous
Limit
200

50,000

6. On how many records we can perform DML operation in a transaction?


A. Batch Apex DML limit is same as normal apex limit i.e. 10,000 records in single transaction.
This means your each batch of batch apex can perform DML upto 10,000 records.
7.What are Asynchronous Operations?
Asynchronous Apex
When to Use
Feature
Future Methods
When you have a long-running method and need to prevent delaying
an Apex transaction
When you make callouts to external Web services
To segregate DML operations and bypass the mixed save DML error
Queueable Apex
To start a long-running operation and get an ID for it

Batch Apex
Scheduled Apex

To pass complex types to a job


To chain jobs
For long-running jobs with large data volumes that need to be
performed in batches, such as database maintenance jobs
For jobs that need larger query results than regular transactions allow
To schedule an Apex class to run on a specific schedule

7.What is Database.Batchable interface?


An instance of a class that implements the Database.Batchable interface. An optional
parameter scope . This parameter specifies the number of records to pass into the execute
method. Use this parameter when you have many operations for each record being passed in and
are running into governor limits.
8. What are the methods in Database.Batchable interface?
Database.Batchable interface has three methods which we must implement as given below:
Start , Execute , Finish
9.What is Database.querylocator?
A.Use the Database.QueryLocator object when you are using a simple query (SELECT) to
generate the scope of objects used in the batch job. If you use a querylocator object, the governor
limit for the total number of records retrieved by SOQL queries is bypassed. For example, a
batch Apex job for the Account object can return a QueryLocator for all account records (up
to 50 million records) in an organization.
10.How many records can be fetched using Database.Querylocator in Start method?
A maximum of 50 million records can be returned in the Database.QueryLocator object. If more
than 50 million records are returned, the batch job is immediately terminated and marked as
Failed.
11.When do you use database.Querylocator as return type for start method?
12.What is iterable<Sobject>interface?
A. Iterable is used when you need to create a complex scope for the batch job. You can also use
the iterable to create your own custom process for iterating through the list."
13.When do you use iterable as return type in start method?
14.How many records can be fetched using iterable?
15.What is iterator?
16.what is Database.batchable context?
A. The execute method is called for each batch of records passed to the method. Use this method
to do all required processing for each chunk of data.
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, the returned list should be used.
Batches of records are not guaranteed to execute in the order they are received from
the start method.
17. what is the use of the Database.batchablecontext?
A. All the methods in the Database.Batchable interface require a reference to
a Database.BatchableContext object. Use this object to track the progress of the batch job.
The following is the instance method with the Database.BatchableContext object:

Name
getJobI
D

Argument
s

Return
s
ID

Description
Returns the ID of the AsyncApexJob object associated
with this batch job as a string. Use this method to track the
progress of records in the batch job. You can also use this
ID with the System.abortJob method.

18.What is the use of Execute() method?


A. To do the required processing for each chunk of data, use the execute method. This method is
called for each batch of records that you pass to it.
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.
Batches of records tend to execute in the order in which theyre received from the start method.
However, the order in which batches of records execute depends on various factors. The order of
execution isnt guaranteed.
19.How many times Execute() method will be called?
20.What is the scope meter in Execute() method?
21.What is the use of finish method?
A. The finish method is called after all batches are processed. Use this method to
send confirmation emails or execute post-processing operations. That means that if at least one
batch wasn't executed we will not enter finish() method.
22.What is the default size of the Batch?
23.What is the minimum size and maximum size of the batch?
A. Minimum size 200 and Maximum size 2000.
24. At a time how many jobs can be added to the Queue?
A. Up to 5 batch jobs can be queued or active.
25.What is the flex concept in Batch?
26.Can we call Batch Apex in another Batch?
A. Yes it is possible, starting with Apex saved using Salesforce API version 26.0, you can call
Database.executeBatch or System.scheduleBatch from the finish method. This enables you to
start or schedule a new batch job when the current batch job finishes.
For previous versions, you cant call Database.executeBatch or System.scheduleBatch from any
batch Apex method. Note that the version used is the version of the running batch class that starts
or schedules another batch job. If the finish method in the running batch class calls a method in a
helper class to start the batch job, the Salesforce API version of the helper class doesnt matter.
Is it possbile to write batch class and schedulable class in a same class?
By implementing Database.Batchable and Schedulable interfaces we can implement the methods
in a same class.
Though it is possible it is not recommended to write like this.
How to cover the code for a batch class?
To cover the code for the batch class we should call the batch class from the inside of the
Test.startTest() and Test.stopTest().?
1 Test.startTest();
2
//Call the batch class from here.

3 Test.stopTest();
Is it possible to call future method from a batch class?
We cannot call one asynchronous process from another asynchronous process.
Since @future method and Batch Class both are asynchronous we cannot call future method
from batch class or we cannot call batch class from the future method.
How to maintain the state between the methods of batch class?
By default batch class is stateless (variable value which is stored in one method cannot be
remembered in another method).
To maintain the state for the batch class, we should inherit Database.Stateful interface.
Scenario: In a set list of emails are stored in execute method. In the finish method that set is not
having any emails. What is the reason?
Answer: By default batch class is stateless. Emails which are added to set can be remembered
only in execute method. If we try to access the set in finish method you won't see those emails.
In finish method if you want to access those emails of that set we should inherit the interface
called Database.Stateful.
How to schedule batch apex in minutes/hours?
To schedule the batch class in minutes/hours, in the finish method we should use
System.schedule method which will take 3 parameters Job Name, Chrone Expression and
schedulable class instance name respectively.
How to schedule batch apex?
To schedule the batch class we should write a separate class by implementing Schedulable
interface.
After writing the above mentioned class to schedule navigates to: Develop> Apex Classes>
Schedule Apex.
By clicking on Schedule Apex button we can schedule the batch class through user interface.
Note: Through user interface we cannot schedule in hours/minutes.
27.Is the state of data is maintained between the Batch Apex execute methods?
28.What is Database.Stateful Interface?
29.When do we use Database.Stateful Interface?
30.If we use Database.Stateful Interface then state of static variables is maintained?
31.Can we call webserivce callouts from the Batch apex?
32.What is the use of Database.AllowCallouts?
33.How many Callouts we can call in each execute()?
34.Can we call future method in execute method of the batch apex?
35.Can a class have both Database.Batchable and ScheduleApex on a same class?

36.When we have divided the job in to five batches and if any one of the batch job fails
what will happen to other jobs?

Interview Questions on Batch Apex In salesforce


1.What are the soql limitations in apex?
Ans: Total number of records retrieved by SOQL Queries-50,000
2.What are the transaction limitations in apex?
Ans: 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 are not rolled back.
3.What is the need of batch apex?
Ans: By using Batch apex classes we can process the records in batches in asynchronously.
4.What is Database.Batchable interface?
Ans: The class that implements this interface can be executed as a batch Apex job.
5.Define the methods in batchable interface?
Ans:
Start:
global Database.Querylocator start (Database.BatchableContext bc){}
Execute:
global void execute(Database.BatchableContext bc,List <p>){}
Finish:
global void finish(Database.BatchableContext bc) {}
6.What is purpose of start method in batch apex?
Ans: It collect the records or objects to be passed to the interface method execute.
7.What is the Database.QueryLocator?
Ans: If we use a Database.QueryLocator,
the governor limit for the total number of records retrieved by SOQL queries is bypassed.
(Default 50,000 It allow up to 50 million records).
8.What is the iterable<Sobject>?
Ans: If you use an iterable,
the governor limit for the total number of records retrieved by SOQL queries is still enforced.
9.What is the use of execute method?
Ans: Contains or calls the main execution logic for the batch job.
10.How many times execute method is called?

Ans: Execute method is called for each batch of records.


11.What is the scope of execute method?
Ans: The maximum value for the optional scope parameter is 2,000
12.Can we call callouts from batch apex?
Ans: Yes we can call.
13.Can we call another batch apex from batch apex?
Ans: Yes you can call a batch apex from another batch apex .Either in start method or in finish
method you can call other batch
14.How many callouts we can call in batch apex?
Ans: Batch executions are limited to one callout per execution.
15.Batch is synchronous or Asynchronous operations?
Ans: Asynchronous operations.
16.What is the maximum size of the batch and minimum size of the batch ?
Ans: The default batch size is 200 records. min?
max?
17.What is the Database.BatchableContext?
Ans: BatchableContext Interface is Represents the parameter type of a batch job method and
contains the batch job ID. This interface is implemented internally by Apex.
18.How to track the details of the current running Batch using BatchableContext?
Ans: You can check the AsyncApexJob.Status using the JobId from the
Database.BatchableContext.
19.How many batch jobs can be added to queue?
Ans: Queued counts toward the limit of 5.
20.What is Database.State full interface?
Ans:To maintain variable value inside the Batch class, Database.Stateful is used.
21.What is Database.AllowCallouts?
Ans:
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.

22.What is AsyncApexJob object?


Ans: AsyncApexJob is Represents an individual Apex sharing recalculation job.
Use this object to query Apex batch jobs in your organization.
23.When a BatchApexworker record is created?
Ans: For each 10,000 AsyncApexJob records, Apex creates one additional AsyncApexJob record
of type BatchApexWorker for internal use.

Potrebbero piacerti anche