global class SearchAndReplace implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('Select id, name, Field_To_Update__c from Account');
}
global void execute(Database.BatchableContext BC, List<Account> scope){
List<Account> accListToUpdate = new List<Account>();
for(Account Acc : scope){
Acc.Field_To_Update__c = Acc.Field_To_Update__c+100;
accListToUpdateadd(Acc);
}
if(accListToUpdate != null && accListToUpdate.size()>0){
update accListToUpdateadd;
}
global void finish(Database.BatchableContext BC){
}
}
But to schedule the Batch in every 5 min you need to create one scheduler class like below
global class scheduledBatchable implements Schedulable
{
global void execute(SchedulableContext sc)
{
SearchAndReplace b = new SearchAndReplace();
database.executebatch(b);
}
}
To Excute scheduler in every 5 min you need to exceute below code i developer console
I am assuming that field on the account Object and you have to update all the records.
Adjut the code as per your requirement -
Thnaks,
Sumit Kumar Singh
All Answers
I am assuming that field on the account Object and you have to update all the records.
Adjut the code as per your requirement -
Thnaks,
Sumit Kumar Singh
here is the code for a batch class that will increment the value of a field by 100 in every 5 minutes
global with sharing class BatchLeadupdateNumberofEmp implements Database.Batchable<sObject> {
global String query{ get; set;}
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id,Name,NumberOfEmployees FROM Lead';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Lead> scope)
{
list<Lead> accList = new list<Lead>();
for(Lead acc: scope){
if(acc.NumberOfEmployees!=null){
acc.NumberOfEmployees +=100;
accList.add(acc);
}
}
update accList;
}
global void finish(Database.BatchableContext BC){
}
}
Now just schedule this batch class for every 5 minutes.
Thanks
Example given my Sumit (+1) good one for you.
Batch Job
But to schedule the Batch in every 5 min you need to create one scheduler class like below To Excute scheduler in every 5 min you need to exceute below code i developer console Check below post for more detail
http://srinusfdc.blogspot.in/2012/10/scheduling-batch-class-for-every-5.html
or Let us k now if this will help you