[SalesForce] put APEX scheduling code

Not for the first time I've drawn a blank with the Salesforce documentation and feel like I'm asking a simple question.

I have written an Apex class that implements Schedulable. The job will be part of a managed package. I would now like to schedule the job. This is the sample code from the documentation. Where should said code be located / run?

I want to run the job frequently, e.g. every 5 minutes.

Documentation:
http://www.salesforce.com/us/developer/docs/apexcode/index.htm

To schedule the job every 5 minutes I could not use standard cron syntax. This is how I got it to work every 5 minutes:

UpdateJobSchedule updater = new UpdateJobSchedule();
    String sch='0 0 * * * ?';
    String jobID = system.schedule('Update job 00', sch, updater);     
    sch='0 5 * * * ?';
    jobID = system.schedule('Update job 05', sch, updater);  
    sch='0 10 * * * ?';
    jobID = system.schedule('Update job 10', sch, updater);  
    sch='0 15 * * * ?';
    jobID = system.schedule('Update job 15', sch, updater);  
    sch='0 20 * * * ?';
    jobID = system.schedule('Update job 20', sch, updater);  
    sch='0 25 * * * ?';
    jobID = system.schedule('Update job 25', sch, updater);  
    sch='0 30 * * * ?';
    jobID = system.schedule('Update job 30', sch, updater);  
    sch='0 35 * * * ?';
    jobID = system.schedule('Update job 35', sch, updater);  
    sch='0 40 * * * ?';
    jobID = system.schedule('Update job 40', sch, updater);  
    sch='0 45 * * * ?';
    jobID = system.schedule('Update job 45', sch, updater);  
    sch='0 50 * * * ?';
    jobID = system.schedule('Update job 50', sch, updater);  
    sch='0 55 * * * ?';
    jobID = system.schedule('Update job 55', sch, updater);

Best Answer

You have a few options:

  • The easiest is to schedule the class to run from the Apex Classes page. Setup > Develop > Apex Classes > Schedule Apex
  • Another option is to create your own scheduling UI using Visualforce and use the code you mentioned above in your controller to schedule apex.
  • Lastly, you can schedule apex in the Execute Anonymous code window of the Developer Console, Workbench, etc. Running apex code anonymously should be done at your own risk as it does not include any tests to ensure proper function.

EDIT: Looks like you added that it was a managed package after I posted my answer. You could put your scheduling code into an install script: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_install_handler.htm

Related Topic