Public class countProcessed implements Database.Batchable<Sobject>,Database.Stateful{
Public integer count = 0;
Public Database.QueryLocator start(Database.BatchableContext bc){
String str ='select id,Name,Rating from Account';
return Database.getQueryLocator (str);
}
Public void execute(Database.BatchableContext bc, List<Account> lAcc){
for(Account a :lAcc){
a.Rating = 'Cold';
}
try{
Database.SaveResult[] s = Database.update(lAcc,false);
for(database.SaveResult d : s){
if(d.isSuccess()){
count++;
}
}
}
catch(exception e){
system.debug('failed');
}
}
Public void Finish(Database.BatchableContext bc){
system.debug(count);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'archanaonline91@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Done');
mail.setPlainTextBody(count +' '+ 'records have been updated sucessfully');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Execution batch job from Developer Console:
countProcessed cp = new countProcessed();
Database.executeBatch(cp);
To check the result -
Comments
Post a Comment