Apex Scheduling

Asynchronous Apex – batch apex, future methods, and scheduled apex


Scheduled Execution of Apex -The Apex Scheduler lets you have Apex classes run at specified times. This is ideal for daily or weekly maintenance tasks.
implements
Schedulable
global void execute(SchedulableContext sc){} - only method in interface schedulable

global class scheduledDelete implements Schedulable {
     
global void execute(SchedulableContext SC) {
       delete [select id from Employee__c];
      }
}


Method 1:
Setup>> Develop >> Apex Classes >> Schedule Apex >> Enter Job Name, select Class, Weekly|Monthly, Start Date, End Date, Preferred Time.

Method 2:
For scheduling a above (or any) schedulable class from developer console

scheduledDelete m = new scheduledDelete ();
String sch = '0 55 16 25 12 ?';
String jobID = system.schedule('Merge Job', sch, m);


The System.Schedule method takes three arguments: a name for the job, an expression used to represent the time and date the job is scheduled to run, and the name of the class. This expression has the following syntax:
Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

The following are some examples of how to use the expression.
ExpressionDescription
0 0 13 * * ?Class runs every day at 1 PM.
0 0 22 ? * 6LClass runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRIClass runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010Class runs every day at 8 PM during the year 2010.

Tracking the Progress of a Scheduled Job Using Queries

After the Apex job has been scheduled, you can obtain more information about it by running a SOQL query on CronTrigger and retrieving some fields, such as the number of times the job has run, and the date and time when the job is scheduled to run again, as shown in this example.
CronTrigger ct = 
    [SELECT TimesTriggered, NextFireTime
    FROM CronTrigger WHERE Id = :jobID];
The previous example assumes you have a jobID variable holding the ID of the job. The System.schedule method returns the job ID. If you’re performing this query inside the execute method of your schedulable class, you can obtain the ID of the current job by calling getTriggerId on the SchedulableContext argument variable. Assuming this variable name is sc, the modified example becomes:
CronTrigger ct = 
    [SELECT TimesTriggered, NextFireTime
    FROM CronTrigger WHERE Id = :sc.getTriggerId()];
You can also get the job’s name and the job’s type from the CronJobDetail record associated with the CronTrigger record. To do so, use the CronJobDetail relationship when performing a query on CronTrigger. This example retrieves the most recent CronTrigger record with the job name and type from CronJobDetail.
CronTrigger job = 
    [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, CronJobDetail.JobType 
    FROM CronTrigger ORDER BY CreatedDate DESC LIMIT 1];
Alternatively, you can query CronJobDetail directly to get the job’s name and type. This next example gets the job’s name and type for the CronTrigger record queried in the previous example. The corresponding CronJobDetail record ID is obtained by the CronJobDetail.Id expression on the CronTrigger record.
CronJobDetail ctd = 
    [SELECT Id, Name, JobType 
    FROM CronJobDetail WHERE Id = :job.CronJobDetail.Id];
To obtain the total count of all Apex scheduled jobs, excluding all other scheduled job types, perform the following query. Note the value '7' is specified for the job type, which corresponds to the scheduled Apex job type.
SELECT COUNT() FROM CronTrigger WHERE CronJobDetail.JobType = '7'



Receiving error "Schedulable class has jobs pending or in progress"
Last updated 26 days ago ·Reference W-1335911 ·Reported By 71 users
Summary
Starting with the Winter 13 Release customers may receive the message "Schedulable class has jobs pending or in progress" when trying to save apex classes related to a scheduled job that is no longer active.
Repro
Schedule more than one job (this only occurs if there are multiple scheduled jobs) then cancel one of them from the setup/monitoring/scheduled jobs screen. If you then try to modify one the classes related to the job just canceled the error will occur.
Workaround
Canceling all jobs, Apex, Reports, Dashboards, etc., that appear in the "Scheduled Jobs" screen will allow the class to be saved. This is because the code causing the issue is not executed if the Scheduled Jobs queue is empty.

Comments

Popular posts