4

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);
4
  • The chron syntax allows for multiple values. You could do this all in one line sch='0 0,5,10,15,20,25,etc * * * ?'; Oct 8, 2013 at 1:37
  • I tried it and it didnt work
    – wisemanIV
    Oct 8, 2013 at 2:57
  • Just making sure but you took out the 'etc' right? Oct 8, 2013 at 3:06
  • Yep. I know what you mean but what I posted was the only way I could get it to work through the anonymous execution window.
    – wisemanIV
    Oct 8, 2013 at 3:17

2 Answers 2

6

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

4
  • I found the schedule apex option in the UI but it only offers weekly and monthly options. I'd like to run this job frequently, like every 10 minutes. Also, I need the schedule to be included in my managed package and executed on installation.
    – wisemanIV
    Oct 7, 2013 at 22:31
  • InstallHandler looks like the best option. Thanks.
    – wisemanIV
    Oct 7, 2013 at 22:32
  • I would still provide some form of interface that an end customer can use to schedule it. I don't think a managed package stops people from deleting the scheduled job in their org, so you'll want to provide some mechanism for an admin to re-enable it with the same schedule as your app requires. Oct 7, 2013 at 22:48
  • Depending on what you're doing, if you schedule something during post-install it's running as a funny not-really-part-of-that-org user and won't have access to most of the org data. I had a cron script that I wanted to create tasks and it didn't work if I started it from a post-install script.
    – Akrikos
    Sep 2, 2014 at 14:25
0

You use developer console to add scheduler code

Go to Developer Console: Debug->Open Execute Anonymous Window

Example - following code executes scheduledCron class for every 15 mins.

System.schedule('Scheduled Job 1', '0 0 * * * ?', new scheduledCron ());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new scheduledCron ());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new scheduledCron ());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new scheduledCron ());
1
  • FYI, this approach is laid out in the existing answer.
    – Adrian Larson
    Jun 23, 2017 at 13:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .