Scheduling Batch Apex Jobs

To schedule a job using the user interface, go to the App Setup area and click Develop, Apex Classes. Click the Schedule Apex button. In Figure 9.3, the Listing9_8 class has been configured to run Saturday mornings at 11:00 a.m. between 7/10/2013 and 8/10/2013.

Image

Figure 9.3 Schedule Apex user interface

To view and cancel scheduled jobs, go to the Administration Setup area and click Monitoring, Scheduled Jobs. This is shown in Figure 9.4 with the previously scheduled job. At this point, you can click Manage to edit the schedule, or Del to cancel it.

Image

Figure 9.4 All Scheduled Jobs user interface

The same management of scheduled jobs available in the user interface can be automated using Apex code, as described next:

Image Create a scheduled job—Use the System.schedule method to schedule a new job. This method requires three arguments: the name of the job, the schedule expression, and an instance of class to schedule. The schedule expression is a string in crontab-like format. This format is a space-delimited list of the following arguments: seconds, minutes, hours, day of month, month, day of week, and year (optional). Each argument is a value specifying when the job is to run in the relevant units. All arguments except seconds and minutes permit multiple values, ranges, wildcards, and increments. For example, the schedule expression 0 0 8 ? * MON-FRI schedules the job for weekdays at 8:00 a.m. The 8 indicates the eighth hour, the question mark leaves day of month unspecified, the asterisk indicates all months, and the day of week is Monday through Friday. The time zone of the user scheduling the job is used to calculate the schedule.


Note

For a full reference to schedule expressions, refer to the Force.com Apex Code Developer’s Guide section on the subject, available at http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_scheduler.htm.


Image View a scheduled job—To get attributes about a scheduled job, such as when it will be executed next, query the standard object CronTrigger. It includes useful fields such as NextFireTime, PreviousFireTime, as well as StartTime and EndTime, calculated from the time the scheduled job was created to the last occurrence as specified by the schedule expression.

Image Delete a scheduled job—The System.abortJob method deletes scheduled jobs. It requires a single argument, the identifier returned by the SchedulableContext getTriggerID method. This can also be obtained from the Id field of a CronTrigger record.

Image Modify a scheduled job—The standard object CronTrigger is read-only, so to modify a job, you must delete it first and then re-create it.

The code in Listing 9.9 can be executed in the Execute Anonymous view to schedule the Listing9_8 class to run monthly on the first day of every month at 1:00 a.m. in the user’s time zone. You can verify this by examining the scheduled job in the user interface or querying the CronTrigger object.

Listing 9.9 Sample Code to Schedule Batch Apex


System.schedule('Scheduled Test', '0 0 1 * * ?', new Listing9_8());



Caution

After an Apex class is scheduled, its code cannot be modified until all of its scheduled jobs are deleted.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.25.60