Professional Documents
Culture Documents
Database.Batchable
Method : start() //execute()//Finish() .
--------------------------------------------------------------------
global class Batch_Example_2 implements Database.Batchable<sobject>{
global Database.QueryLocator start(Database.BatchableContext bc){
string query='select id,name, type from Opportunity';
return Database.getQueryLocator(query); //this can retrun upto 50 Milion
records
}
//Batch1 Batch2 Batch3 Batch4
Batch5
//0-100 101-200 201-300 301-
400 401-500
//execute(scope) execute(scope) execute(scope) execute(scope)
execute(scope)
global void execute(Database.BatchableContext bc,List<Opportunity> scope){
for(Opportunity op:scope){
op.type='New Customer';
}
update scope;
//system.debug('subJob:'+bc.getChildJobId());
}
global void finish(Database.BatchableContext bc){
AsyncApexJob job=[select id,status,JobType from AsyncApexJob where Id=:
bc.getJobId()];
system.debug('Status:'+job.Status);
}
}
---->
Apex code:Open Execute Anonomous Window.
Batch_Example_2 be=new Batch_Example_2();
Id jobId=Database.executebatch(be,5);
---->
// default size:200
Id jobId=Database.executebatch(be,10); // 10-10 records per batch.
[size 1-2000]
------->
set -- job-> Apex Jobs.
1]when you going through larger record then you hit the govener limit so we used
apex batch.
2]Batch Apex operates over small batches of records, covering your entire record
set and breaking the processing down to manageable chunks.
3]Batch Apex allows you to handle more number of records and manipulate them by
using a specific syntax.
4] Method : start() //execute()//Finish() .
-------->
Database.Bathcable --> Interface #interface always global so we used global
class.
start,execute,finish
Database.BatchableContext---> getJob, getChildJobId().
Database.stateful[interface]
Database.AllowsCallouts [interface for the webservice]
What is BatchApex:
saleforce implemating the architecture of multitant to avoide dominance of
perticular applications , to implemanting this mult
arechitecture there are same limitation called governer limits like total number
DML-150 webservie-100, No.SOOL-40, Email inviquation -10,
Queue size-60 lac.
but when you trying to work large data,huge data then using batchApex, batchApex
will break the operation into sub operations [job in subjob]
every job/subjob run independetly.
-------------------
#Create a bath apex which will fetch all the accounts and update rating as hot.
global class Batch_Example_3 implements
Database.Batchable<sobject>,Database.stateful {
global integer count=0;
global static integer size=0;
global Database.QueryLocator start(Database.BatchableContext bc){
string query='select id,name from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Account> scope){
for(Account a:scope){
count=count+1;
size=size+1;
}
}
global void finish(Database.BatchableContext bc){
Account acc=new Account();
acc.Name='BatchApex';
acc.Description='count :'+count+ 'Size :'+Size;
insert acc;
}
}
-->
Apex code:Open Execute Anonomous Window.
Batch_Example_3 be=new Batch_Example_3();
Id jobId=Database.executebatch(be,10);
-----------------------------------------------------------------------------------
-----------------------------------------
global class Bath_Example_4 implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'Select id,name, type from Account where type=null';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> Scope){
for(Account a:scope){
a.type='Prospect';
}
update scope;
}
global void finish(Database.BatchableContext BC){
system.debug('Account Update Type=prospect');
}
}
-------->
Bath_Example_4 acc=new Bath_Example_4();
Database.executeBatch(BC);
-----------------------------------------------------------------------------------
------------------------------------------------
global class Batch_Example_5 implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
string query='select First_Name__c,Gender__c from Staff_Detail__c';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Staff_Detail__c> scope){
for(Staff_Detail__c s:scope){
if(s.Gender__c=='Male'){
s.First_Name__c='Mr.'+s.First_Name__c;
} else{
if(s.Gender__c=='Female'){
s.First_Name__c='Miss.'+s.First_Name__c;
}
}
}
update scope;
}
global void finish(Database.BatchableContext BC){
System.debug('Update the Saluasation of Staff');
}
}
------->
Batch_Example_5 BC=new Batch_Example_5();
Database.executeBatch(BC,5); //5 record per Batch.
---------------------------------------------------------------------------------
global class Batch_Example_6 implements Database.Batchable<sobject> {
global Database.QueryLocator start(Database.BatchableContext BC){
string Query='Select First_Name__c,Gender__c from students_details__c';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<students_details__c>
scope){
for(students_details__c s:scope){
if(s.Gender__c =='Male'){
s.First_Name__c='Mr.'+s.First_Name__c;
} else if(s.Gender__c=='Female')
{
s.First_Name__c='Miss.'+s.First_Name__c;
}
Database.update(scope);
}
}
global void finish(Database.BatchableContext BC){
system.debug('Salutation Update to Student Details');
}
}
------>
Batch_Example_6 BC=new Batch_Example_6();
Database.executeBatch(BC,2);
-----------------------------------------------------------------------------------
-----------
/* Develope a simple Batch Apex which will
* Update the contact Fax with Account Fax if Contact Fax is Null
* Update contact mobile phone with Account Phone if contact phone is Null
* Update Email With atulkhope.sfdc@gmail.com
* Update description with Account Name +Account Industry +Rating +contact Email
* Send the Notification to user atulkhope.sfdc@gmail.com when batch executed
successfully
* user of Database.stateful interface to send the information about successs and
failed records*/
-----------------------------------------------------------------------------------
---------------------------------