global class BatchClassExample implements Database.Batchable<sObject>  
  global integer i;  
  global list<sObject> start(Database.BatchableContext BC)  
     list<Opportunity> oList=new list<Opportunity>([select id from opportunity where StageName='Closed Won']);  
    return oList;  
  global void execute(Database.BatchableContext BC, list<opportunity> oList)  
    Set<Id> opSet = new Set<Id>();       
    for(opportunity op:oList)    
    AggregateResult[] results=[select count(id) from opportunity where id in:opSet];  
    for(AggregateResult ar:results)  
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
      String[] toAddresses = new String[] {'richa.a.shrivastava@accenture.com'};     
      mail.setSubject('Count Of Opportunities closing today ');  
      mail.setPlainTextBody('Closed opportunities for today are: ' +i);  
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail} );  
  global void finish(Database.BatchableContext BC)  

global class ScheduleBatchClass implements schedulable  
 ScheduleBatchClass sBC =new ScheduleBatchClass();  
 global void execute(SchedulableContext sc)  
   BatchClassExample  bCE= new BatchClassExample ();  
   Integer scopeSize = 200;  
  Datetime sysTime = System.now().addSeconds(60);        
  String sch= '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();  
  String jobID = system.schedule('Batch Scheduled Job', sch, sBC);  
  • Can someone tell me, where am i going wrong? – pluto Aug 25 '14 at 8:09
  • What do you want to do ? What's the errors displayed ? Please provide more informations ! – SF_user Aug 25 '14 at 8:23
  • i have to send an email daily, giving count of closed opportunities using batch scheduling. And the code has been compiled successfully, but i am not getting any email!! – pluto Aug 25 '14 at 8:28

I don't see anything wrong with the email sending code. But it looks like you are not calling Database.executeBatch anywhere so the batch will never run. Personally, I would just use the "Schedule Apex" button to schedule this ScheduleBatchClass:

global class ScheduleBatchClass implements Schedulable {
    global void execute(SchedulableContext sc) {
        Integer scopeSize = 5000;
        Database.executeBatch(new BatchClassExample(), scopeSize);  

There are also these two problems in the counting logic:

  • the class needs to implement a second marker interface implements Database.Batchable<sObject>, Database.Stateful { so that the value of i is preserved across batches
  • as the work is likely to be broken up into many batches, the total needs to be incremented not assigned i += Integer.ValueOf(ar.get('expr0')); and so the value needs to be initialized global integer i = 0;
  • 1
    Still have to implement Schedulable in BatchClassExample to use "Scheduled Apex" won't they? – Phil Hawthorn Aug 25 '14 at 9:30
  • You are right - thank I'll update the answer. – Keith C Aug 25 '14 at 9:48
  • @ Keith C oops,sorry i forgot to add Database.executeBatch method. But now i have modified the code. Still it is not sending any mail. – pluto Aug 25 '14 at 10:23
  • @pluto Do address the other points too. But at this point you'll have to start doing some debugging unless anyone else can see the problem - here is sone info on how to do that. – Keith C Aug 25 '14 at 10:42
  • I have uploaded the modified code. I am receiving the mail regarding closed opportunities when i run the code in execute anonymous. But somehow it is not getting scheduled. Could someone help? – pluto Aug 26 '14 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.