+ Start a Discussion
test777test777 

Urgent:Scheduling an apex class for each 5 minutes

Hi all,

   i am new to apex scheduling class.I need to write apex class for scheduling each 5 minutes.Any one can u please help me this.

 

 

Thanks in advance.

anu

IspitaIspita

Hi ,

To invoke Apex classes to run at specific times, first implement the Schedulable interface for the class, then specify the schedule using either the Schedule Apex page in the Salesforce user interface, or theSystem.schedule method.

For more information about the Schedule Apex page, see “Scheduling Apex” in the Salesforce online help.

 

Implementing the Schedulable Interface
To schedule an Apex class to run at regular intervals, first write an Apex class that implements the Salesforce-provided interface Schedulable.
The scheduler runs as system: all classes are executed, whether the user has permission to execute the class or not. For more information on setting class permissions, see “Apex Class Security Overview” in the Salesforce online help.

 

The Schedulable interface contains one method that must be implemented, execute.

global void execute(SchedulableContext sc){}

 

The following example implements the Schedulable interface for a class called mergeNumbers:
 global class scheduledMerge implements Schedulable{
   global void execute(SchedulableContext SC) {
      mergeNumbers M = new mergeNumbers(); 
   }
}
The following example uses the System.Schedule method to implement the above class.
 scheduledMerge m = new scheduledMerge();
        String sch = '20 30 8 10 2 ?';
        system.schedule('Merge Job', sch, m);
You can also use the Schedulable interface with batch Apex classes. The following example implements the Schedulable interface for a batch Apex class called batchable:
 global class scheduledBatchable implements Schedulable{
   global void execute(SchedulableContext sc) {
      batchable b = new batchable(); 
      database.executebatch(b);
   }
}

 

Helpful links:-

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

UVUV

You must keep in mind the batch apex limits as you may hit the limits by scheduling the batch apex after every 5 mins.

 

  • Up to five queued or active batch jobs are allowed for Apex.
  • A user can have up to five query cursors open at a time. For example, if five cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the five cursors is released.

    Cursor limits for different Force.com features are tracked separately. For example, you can have five Apex query cursors, five batch cursors, and five Visualforce cursors open at the same time.

  • A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
  • The maximum value for the optional scope parameter is 400. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of 400.
  • If no size is specified with the optional scope parameter, Salesforce chunks the records returned by the QueryLocator into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.
  • The start, execute and finish methods can implement only one callout in each method.
  • Batch executions are limited to one callout per execution.
  • The maximum number of batch executions is 250,000 per 24 hours.
  • Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn’t cause any batch job to fail and execute methods of batchApex jobs still run in parallel if more than one job is running.
Tom NiemanTom Nieman
I don't understand the lines 7-10.  Do those just get run in an immediate window?