Concepts or coding lessons of Salesforce that you can implement easily

Run Schedule a Class In Every 5 Mins in Salesforce

You can run apex job every 1-hour using CRON expression by default but you can schedule this job 12 times in one hour at 5 min duration.

If you are not aware of Schedulable Batch Apex then first look Schedulable Batch Apex In 3 Easy steps In Salesforce


Then how to Schedule a Class in Every 5 Mins in Salesforce, since this is not possible to do through the standard Salesforce User interface.

Yes, I have a solution for it. So here is a small code to do so for every 5 minutes.

Example, you have Scheduler class ScheduleBatchApexClassExample which is in my other blog and want to run the schedule a class in every five mins, then use below CRON expression.

How to run follow below process:


Go to your developer console -> Open Execute Anonymous Window -> Copy and Paste Below code and click on execute button

Simply say, Run below CRON expression from your developer console:



System.schedule('Schedule Job Name 1',  '0 00 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 2',  '0 05 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 3',  '0 10 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 4',  '0 15 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 5',  '0 20 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 6',  '0 25 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 7',  '0 30 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 8',  '0 35 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 9',  '0 40 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 10', '0 45 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 11', '0 50 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 12', '0 55 * * * ?', new ScheduleBatchApexClassExample());

After this, if you want To check apex class in scheduled or not then:
Go to -> Setup -> Jobs -> Scheduled Jobs















So Easy !!!

Some important things of System.schedule :


To Run Schedule a Class In Every 5 Mins in Salesforce, you have to use System.schedule method. System.schedule method is used to executes a Apex class. 

System.schedule('ScheduleBatchApexClassExampleScheduler', '0 0 * * * ?', new ScheduleBatchApexClassExample());

If you want to see your batch job scheduled then 
  • Go to Setup—>Monitor –> Scheduled Jobs
Syntax for CRON expression:

Seconds Minutes Hours Day Month Week Year


Year is optional.

The following are some examples of cron expressions:
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.

Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

loading...

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Using your code I get an "invalid type: ScheduleBatchApexClassExample" when I try to schedule through Execute Anonymous. I'm only working in one dev org, and there are no namespace prefixes...
    Any suggestions here?

    ReplyDelete
  3. ^ignore above! copied the whole thing again, and it worked.

    ReplyDelete
  4. Thanks Nitish for simple and clear steps...

    ReplyDelete