Ex:1
Apex Class:
Global class batchTesting implements database.batchable<sobject>
{
global database.querylocator start(database.batchableContext bc){
return database.getQueryLocator('select name,city__c from Employee__c limit 10');
}
global void execute(database.batchableContext bc,List<Employee__c> emps){
for(Integer i=0;i<emps.size();i++){
if(emps[i].city__c=='Mumbai'){
emps[i].city__c='Hyd';
}
}
update emps;
}
global void finish(database.batchableContext bc){
system.debug('****Updation Completed');
}
}
developer console code:
batchTesting obj = new batchTesting ();
database.executebatch(obj);
Ex:2
Apex Class:
Global class scheduleTest implements schedulable
{
global void execute(schedulableContext sc){
// scheduling logic..
Employee__c objE = new Employee__c();
objE.name ='Sachin Tendulkar';
objE.city__c='Mumbai';
objE.country__c='India';
insert objE;
}
}
Developer console:
String sch = '1 30 23 20 2 ?';
system.schedule('Merge Job', sch, m);
Ex:3
Apex Class:
global class batchcls implements Database.Batchable<sobject>, Schedulable{
global void execute(SchedulableContext sc){
batchcls obj= new batchcls();
database.executebatch(obj,1);
}
global List<Account> start(database.batchableContext bc){
/*
List<Account> lstacc = new List<Account>();
for(integer i=0; i<11000;i++){
Account ac = new Account();
ac.name='TestAccount'+i;
lstacc.add(ac);
} */
List<Account> lst= [Select id from Account where name like '%TestAccount%'];
return lst;
}
global void execute(database.batchableContext bc, List<Account> lst){
//insert lst;
delete lst;
}
global void finish(database.batchableContext bc){
}
}
Ex:4
Apex Class:
global class batchtesting implements database.batchable<sobject>{
global string query;
global batchtesting(){
query='select name,Number_of_hrs_per_day__c from Batch__c';
}
global database.querylocator start(database.batchablecontext bc){
return database.getquerylocator(query);
}
global void execute(database.batchableContext bc,List<Batch__c> bts){
for(integer i=0;i<bts.size();i++){
if(bts[i].Number_of_hrs_per_day__c == null || bts[i].name =='nay'){
bts[i].Number_of_hrs_per_day__c = 25;
}
}
update bts;
}
global void finish(database.batchableContext bc){
}
static TestMethod void myUnitTest() {
Test.StartTest();
batchtesting upc = new batchtesting();
upc.query = upc.query +'Limit 200' ;
ID batchprocessid = Database.executeBatch(upc);
list<batch__c> bts=new list<batch__c>();
Test.StopTest();
system.debug('****Updation Completed');
}
}
No comments:
Post a Comment