function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dinesh PathakDinesh Pathak 

How to schedule class in Apex

I want to schedule a class for every 5 min.
how to implement this.
Best Answer chosen by Dinesh Pathak
Suraj Tripathi 47Suraj Tripathi 47
Hi Dinesh,
Greetings!

You can do this even from a single Scheduled Job.
In your class.
1. Before execution of your code, fetch the current job from CRONTRIGGER.
2. Abort the job System.abortJob(cronId).
3. Now, Schedule the new job for after 5 min.
4. Execute your schedule class code.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi

All Answers

Sandhya V RaoSandhya V Rao
Go to Developer Console -> Execute Anonymous -> Paste the below code -> Select the entire code -> Click on “Execute Highlighted” (You can also just click “Execute”. However, if there are also other code on execute anonymous, it is always safer to run only the code you have highlighted).

Let’s say your batch class name is BatchApexIllustration.

System.schedule('Schedule Job Now',  '0 00 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 1',  '0 5 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 2',  '0 10 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 3',  '0 15 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 4',  '0 20 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 5',  '0 25 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 6',  '0 30 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 7',  '0 35 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 8',  '0 40 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 9',  '0 45 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 10', '0 50 * * * ?', new BatchApexIllustration());
System.schedule('Schedule Job after 5 minutes - 11', '0 55 * * * ?', new BatchApexIllustration());

In the above code we have used what is called the CRON expression - example '0 5 * * * ?'. To understand more on Apex Scheduler and the expression, refer this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Suraj Tripathi 47Suraj Tripathi 47
Hi Dinesh,
Greetings!

You can do this even from a single Scheduled Job.
In your class.
1. Before execution of your code, fetch the current job from CRONTRIGGER.
2. Abort the job System.abortJob(cronId).
3. Now, Schedule the new job for after 5 min.
4. Execute your schedule class code.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
This was selected as the best answer
AbhinavAbhinav (Salesforce Developers) 
Hi Dinesh,

Please refer this link

https://howtodoitinsalesforce.blogspot.com/2016/12/run-schedule-class-in-every-5-mins-in.html

Thanks!