===================================================================================
=====
Batch Apex
===================================================================================
=====
[Link] there is a need to process large number of records on daily basis or nightly
basis or
specific time of interval then there is a possibilty of encountering the
governing limits .
[Link] resolve the governing limits issue ,we will run the operation as asynchronous
operation using batch apex .
3. Batch Apex is exposed as an interface that must be implemented by the developer.
Batch jobs can be programmatically invoked at runtime using Apex.
4. Batch Apex will break the larger set of records into no of batches with small
set of data and
every batch will run independent from each other with fresh set of governing
limits.
5. To run any Apex class asynchronously using Batch Apex, then that Apex class has
to
implement an Interface called '[Link]' interface.
6. Any Apex class which implements [Link] interaface should define
three methods
[Link]
[Link]
[Link]
About start() method:
--------------------------------
a. This method will be called at the starting of the Batch Job and collects the
data on
which the Batch job need to be operated.
[Link] : [Link] | Iterable<SObject> start
([Link] bc){}
[Link] : When you’re using a simple query (SELECT) to generate
the scope of records on which batch job should run, use the [Link]
d. The QueryLocator is a class which would store the results of getQueryLocator
method as below.
[Link](soql). This method would able to fetch upto 50 Million
records in soql query.
e. 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.
f. If you are using iterable all governing limits will be still enforced.
g. Start method will break the list of records into no of batches and invoke the
execute method on every batch.
About execute() method:
--------------------------------
a. This method will be invoked by the start method on every batch of recods.
b. This method will contain business logic that need to performed on the records
fetched from the start method.
c. Syntax : void execute([Link] bc,List<Sobject> scope)
d. List<sObject> scope is used to hold the data from the start method.
Example:
If the start method has got 1000 records, then these records would be divided into
five batches with 200 records in each batch.
Then execute method will be called on every batch separatly (execute method will be
called 5 times).
Each execute batch will run asynchronously as seperate transaction.
Hence each transaction has its own governor limits.
About finish() method:
----------------------------------
a. This method will be called after executing all the execute method are completed.
b. This method is used to send confirmation emails or post batch operations
c. Syntax : void finish([Link])
Example 1: Fetch all the opportunity which are created today with StageName as
Closed Won and Update their CloseDate.
===================================================================================
=====================================
Note: As per my business requirement there maybe 15k-20k opportunity records which
will be closed every day.
But as per governor limits we can perform DML operations on 10,000 records in one
single transaction.
Hence we are going to implement the above scenario with batch apex.
Solution 1:
-------------------------
public class BatchDemo implements [Link]<sObject>{
public [Link] start([Link] bc){
[Link] ql = [Link]('Select Id, CloseDate,
StageName From Opportunity Where StageName=\'Closed Won\' and CreatedDate=Today');
return ql;
}
public void execute([Link] bc, List<Opportunity> scope){
for(Opportunity opp: scope){
[Link] = [Link]();
}
update scope;
}
public void finish([Link] bc){
[Link] email1 = new
[Link]();
String[] toadd = new String[]{'sanjaycloudcrm@[Link]',
'abc@[Link]'};
[Link](toadd);
[Link]('Batch Completion Alert');
[Link]('Batch Job '+ [Link]() + ' is
processed successfuly');
[Link][] emails=new [Link][]{email1};
[Link](emails);
}
How to run the batch apex?
-------------------------------
Syntax: BatchClassName objectName = new BatchClassName();
[Link](objectName, batchSize);
Example: BatchDemo bd = new BatchDemo();
[Link](bd, 200);
Example 2: Delete the opportunity records that are closed lost in every previous
week.
===================================================================================
=======
public class BatchApexDemo1 implements [Link]<sObject>{
public [Link] start([Link] bc){
[Link] result = [Link]('Select Id From
Opportunity Where stageName = \'Closed Lost\' AND lastModifiedDate = LAST_WEEK');
// 50 Million
return result;
//1000 : 200, 200, 200, 200, 200
}
public void execute([Link] bc, List<Opportunity> oppList){
delete oppList; //10000
}
public void finish([Link] bc){
//send email to manager about the status of the job
AsyncApexJOb jobStatus = [Select status from AsyncApexJOb Where Id
=:[Link]()];
}
}
Considerations:
========================
1. In a batch job if any of the batch fails, then only that batch will fail but the
rest of the batch would be executed normally.
2. If the finish method fails, then only finish method will fail and changes made
by the all the execute methods will be committed.
3. We cannot call future method from the batch apex .
4. We can call batch job from finish method of another batch job.
5. If we want to call webservices from the batch apex batch apex ,Apex class has to
implements [Link] interface.