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

How to create a Schedule Job which runs after specific interval (in hours, minutes, seconds etc)

Hi Folks,


Requirement: To create a Schedule/Cron/Code job which executes after a specific interval, say after every 1 minute.


There is no direct feature in SFDC of doing it but with a tricky way.

Step 1: Create an Apex class which contains the business logic which is required to be executed after specific intervals.

Step 2: Create a Scheduleable Apex Class Like below:


global class ScheduleChatter implements Schedulable {
    global void execute(SchedulableContext SC)  {                 

        //The line of code below would contain the required business logic



        String hour = String.valueOf(;

        String min = String.valueOf( + 1);

        String ss = String.valueOf(; 

String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';

        System.debug('-----------nextFireTime---------' + nextFireTime);


ScheduleChatter s = new ScheduleChatter();

         System.schedule('Job Started At ' + String.valueOf(, nextFireTime, s);





Step 3: This Schedule job is initiated by a code, which can be run on System log screen or via another trigger/class:

ScheduleChatter s = new ScheduleChatter();

String hour = String.valueOf(;

String min = String.valueOf(;

String ss = String.valueOf(;

String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';

system.schedule('Start me once', nextFireTime, s); 


The Schedulable class will keep on calling itself after every 1 minute and eventually calls the code which is needed to be called after specified interval of time.


This code is written as part of R&D.





Hello sam,


Thanks to give this valueable code for us.


but got a error when i reached max scheduled apex.

caused by: System.UnexpectedException: You have exceeded the maximum number (10) of Apex scheduled jobs.


This error is coming  because every time we create a new scheduled job. so How can we delete completed scheduled job. we can't rech at max 10.




It's worth adding here that the answer to the above question is to use the System.abortJob() method to delete unwanted scheduled jobs. The scheduled job id can be used or a job id can be retrieved from querying CronTrigger.

samapika singh 3samapika singh 3
Thanks a Lot @nelloC. I used this in one of the scenarios where for any exception in dml operation I need to resubmit after 1 minute. Your code helped me a lot

Divya MallDivya Mall
Can you help me that how to how to create Scheduler in salesforce which could run for 1 min time interval and how execute and check it 
Hi Sam

Try something like the following:

step 1. Create Batch class.
        for example, I have a batch class that updates the Account "Count" field(Create this field) with all the contacts related to that Account

global class BatchUpadteAccountFieldNumberOfContacts implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query='SELECT Id FROM Account ';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Account> accList) {
        Map<Id,Integer> mapAccountIdContacts = new Map<Id,Integer>(); 
        List<account> AccountContactList = new List<account>();
        AccountContactList = [select id, (select id from contacts) from account];
        for(account accObj:AccountContactList) {
        List<account> newAccountListUpdate = new List<account>(); 
        for(Account accObj : AccountContactList) {
            if(mapAccountIdContacts.get( != null) { 
                accObj.Number_of_Contacts__c = mapAccountIdContacts.get(;
        Update newAccountListUpdate;    
        global void finish(Database.BatchableContext BC) {   
Step 2: Write the Script to Schedule the above class(BatchUpadteAccountFieldNumberOfContacts) .
global class SchedulerUpadteAccountField implements Schedulable{
    global void execute(SchedulableContext SC){
    // create object of batch class BatchUpadteAccountFieldNumberOfContacts
        BatchUpadteAccountFieldNumberOfContacts obj = new BatchUpadteAccountFieldNumberOfContacts();
        // execute batch class

Step 2: Write the Cron expression for the above class(SchedulerUpadteAccountField) to run in every day.
SchedulerUpadteAccountField schedulerObj = new SchedulerUpadteAccountField();
String sch =     '0 0 0 * * ?';
String jobID = system.schedule('Create Opportunity BAtch Job', sch, schedulerObj);