function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hamayoun65hamayoun65 

Using Batch Apex in a Scheduled way

Hi

 

I would like a simple answer to this. We are going to create a batch job, which will calculate some totals on a hierarchical custom object.  We want the batch job to run every day.  Can it be scheduled using the Scheduler?  If so, does anyone have an example of how?  If not, how can this be done, preferably with details?

 

Thx,

Hamayoun 

Best Answer chosen by Admin (Salesforce Developers) 
shillyershillyer

Yes - from the help:

 

Batch jobs can also be programmatically scheduled to run at specific times using the Apex scheduler, or scheduled using the Schedule Apex page in the Salesforce.com user interface. For more information on the Schedule Apex page, see “Scheduling Apex” in the Salesforce.com online help.

 

Best,

Sati

All Answers

shillyershillyer

Yes, in Spring 10, you will be able to schedule apex code. More here.

 

Hope that helps,

Sati

hamayoun65hamayoun65
What I mean is, can I use the Scheduler to execute a Batch Apex class?
hamayoun65hamayoun65

Something like this:

 

global class scheduledMerge implements Schedulable{ 
global void execute(SchedulableContext SC) {
BatchApexClass bac = new BatchApexClass() ;
Database.executeBatch(rap);
}
}

 

shillyershillyer

Yes - from the help:

 

Batch jobs can also be programmatically scheduled to run at specific times using the Apex scheduler, or scheduled using the Schedule Apex page in the Salesforce.com user interface. For more information on the Schedule Apex page, see “Scheduling Apex” in the Salesforce.com online help.

 

Best,

Sati

This was selected as the best answer
hamayoun65hamayoun65

So I tried this, and it does not seem to be quiet working correctly.  Here is the code for my scheduledClass:

 

global class scheduledXXX implements Schedulable{ 
  global void execute(SchedulableContext SC) {
     testBatch tB = new testBatch() ;
     Database.executeBatch(tb);
  }
}

Here is the code for testBatch:

 

global class testBatch implements Database.Batchable<sObject>{

   global testBatch(){
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
        String query ;
      return Database.getQueryLocator('select Id from Accounts');
   }

   global void execute(Database.BatchableContext BC, 
                       List<sObject> scope){
      for(Sobject s : scope){
        Account a = (Account) s ;
System.debug ('Id is ' + a.Id) ;
        }
   }

   global void finish(Database.BatchableContext BC){
    // Get the ID of the AsyncApexJob representing this batch job 
        
       // from Database.BatchableContext. 
        
       // Query the AsyncApexJob object to retrieve the current job's information. 
        
       AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
          TotalJobItems, CreatedBy.Email
          from AsyncApexJob where Id =:BC.getJobId()];
       // Send an email to the Apex job's submitter notifying of job completion. 
        
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAddresses = new String[] {'xxx@yyy.com'};
       mail.setToAddresses(toAddresses);
       mail.setSubject('Test Batch ' + a.Status);
       mail.setPlainTextBody
       ('The batch Apex job processed ' + a.TotalJobItems +
       ' batches with '+ a.NumberOfErrors + ' failures.');
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


   }



}

Monitoring scheduled jobs shows that the scheduled job has been submiitted (which I did via the UI) and started:

 

ActionJob NameSorted AscendingSubmitted BySubmittedStartedNext Scheduled RunType
DelTest SchedBatchUser, Admin2/4/2010 11:41 AM2/4/2010 12:00 PM Scheduled Apex

 

Monitoring Apex Jobs shows that the  batch job is queued.  As of now, it has been queued for about 18 hours.

 

ActionSubmitted DateSorted DescendingJob TypeStatusTotal BatchesBatches ProcessedFailuresSubmitted ByCompletion DateApex ClassApex Method
Abort2/4/2010 12:00 PMBatch ApexQueued000User, Admin testBatch 

 

 

So what is going on?

 

 

Message Edited by hamayoun65 on 02-05-2010 06:10 AM