Batch Apex Flashcards
Learn about Batch Apex and how it differs from other Asynchronous Operations
How does batch apex work ?
Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks.
Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex.
How many queued or active batch jobs you can have at one time ?
You can have only five queued or active batch jobs at one time
How to evaluate current count of active/queued batch jobs?
You can evaluate the current count by viewing the Scheduled Jobs page in Salesforce or
Programmatically using SOAP API to query the AsyncApexJob object.
How to create a Batch Apex Class ?
To use batch apex,
write an Apex Class that implements the Salesforce-provided interface ‘Database.Batchable’ and then invoke the class programmatically.
How to monitor or stop execution of a Batch Apex Job?
To monitor or stop the execution of the batch Apex job from Setup, enter Apex Jobs
in the Quick Find
box. then select Apex Jobs
What are the three methods that must be implemented in the Database.Batchable interface ?
- start method
public (Database.QueryLocator | Iterable<sObject> ) start(Database.BatchableContext bc) {}
- execute method
public void execute(Database.BatchableContext BC,list<p>){}
- finish method
public void finish(Database.BatchableContext BC){}
What is the significance of using the start method in Batch Apex ?
To collect the records or objects to pass to the interface method execute
, call the start
method at beginning of batch Apex job.
Method either returns a Database.QueryLocator
object or an iterable that contains the records or objects passed to the job.
When to use Database.QueryLocator
instead of Iterable
in start method in Batch Apex ?
When you’re using a simple query (SELECT) to generate the scope of objects in the batch job, use the Database.QueryLocator
object.
If you use a QueryLocator
object, the governor limit for the total number of records retrieved by SOQL queries is bypassed.
When to use Iterable instead of Database.QueryLocator
Object in start method in Batch Apex ?
Use the iterable 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.
How do enforcement of governor limits differ between Database.QueryLocator
object and Iterable
object in start method in Batch Apex ?
If you use an iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.
What is the execute
method used in Batch Apex for ?
public void execute(Database.BatchableContext BC,List<p>){}
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 parameters:
1. A reference to the Database.BatchableContext
object.
2. A list of sObjects, such as List<sObject> or a list of parameterized types. If you're using a `Database.QueryLocator`, use the returned list.</sObject>
Is there a set order of execution for the batches in Batch Apex ?
Batches of records tend to execute in the order in which they’re received from the start
method. The order in which batches of records execute depends on various factors. The order of execution isn’t guaranteed.
What is the finish
method in Batch Apex used for ?
public void finish(Database.BatchableContext BC)
This method is called after all batches are processed.
Use this method to send confirmation emails or execute post-processing operations.
Is each execution of a batch Apex job considered a discrete transaction ?
Each execution of a batch Apex job is considered a discrete transaction.
For example,
a batch Apex job that contains 1000 records and is executed without the optional scope
parameter from Database.executeBatch
is considered five transactions of 200 records.
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.
All methods in the Database.Batchable
interface require a reference to which object ?
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.
Which instance method of the Database.BatchableContext
object is used for tracking the progress of records in the batch job ?
getJobID
is the instance method used for tracking the progress of a batch job.
returns : 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.
Example of using Database.BatchableContext
to query the AsyncApexJob associated with the batch job ?
public void finish(Database.BatchableContext BC){ // Get the ID of the AsyncApexJob representing this batch job // from Database.BatchableContext. // Query the AsyncApexJob object to retrieve the current job's information. 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('Apex Sharing Recalculation ' + a.Status); mail.setPlainTextBody ('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); }
How to use Iterable in Batch Apex to define Scope ?
The start
method can return either a Database.QueryLocator
object that contains the records to use in the batch job or an iterable. Use an iterable to step through the returned items more easily.
Give an example of a batch apex class using Iterable
public class batchClass implements Database.batchable{ public Iterable start(Database.BatchableContext info){ return new CustomAccountIterable(); } public void execute(Database.BatchableContext info, List<Account> scope){ List<Account> accsToUpdate = new List<Account>(); for(Account a : scope){ a.Name = 'true'; a.NumberOfEmployees = 70; accsToUpdate.add(a); } update accsToUpdate; } public void finish(Database.BatchableContext info){ } }
Which method is used to submit Batch Jobs ?
Use the Database.executeBatch
method to submit batch jobs.
The Database.executeBatch
method takes two parameters:
- 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.
What is the significance of the optional scope
parameter in Database.executeBatch
method ?
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.
By limiting the number of records , you are limiting the operations per transaction. This value must be greated than zero.
If the start
method of the Batch Class returns a QueryLocator, the optional scope parameter of Database.executeBatch
can have a maximum value of 2000.
If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2000 records.
If the start
method of the Batch class returns an iterable, the scope parameter value has no upper limit. However, if you use a high number, you can run into other limits. The optimal scope size is a factor of 2000, for example,100,200,400, and so on.