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
Ketan MahetKetan Mahet 

SFDC Schedule a job running every 5 mins through out day - month - year

I want to schedule a job runs every 5 mins. I have written the following code it is working fine but as the minutes reaches to 0, it is not creating any new job. so for the hour the job start it works fine till that hour finishes. I want to keep it running through out the day month and year.

Apex Class:
global class scheduledTest implements Schedulable{
    global void execute(SchedulableContext SC) {
        
        
        testMethod();
        
        DateTime dtNow = system.now();
        
        String day = string.valueOf(dtNow.day());
        String month = string.valueOf(dtNow.month());
        String hour =  string.valueOf(dtNow.hour());
        String minute = string.valueOf(system.now().minute() + 5);
        String second = string.valueOf(system.now().second());
        String year = string.valueOf(system.now().year());
        
        String strJobName = 'DistJob-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        //String strSchedule = '0 ' + minute + ' * * * ?';
        System.schedule(strJobName, strSchedule, new scheduledTest());
    }
    
    private void testMethod(){
        system.debug('>>>>>' + system.now());
    }
}

From Dev console I am running the job only once as below:
scheduledTest jb = new scheduledTest();
jb.execute(null);
 
Best Answer chosen by Ketan Mahet
Ketan MahetKetan Mahet
Hi,
   Finally I found an answer with Caveat: (After 100 schedule job, I will throw exception "You have exceeded the maximum number (100) of Apex scheduled jobs"). I am working on it.

global class scheduledTesting implements Schedulable{
    global void execute(SchedulableContext SC) {
        
        
        test_Method();
        
        DateTime dtNow = System.Now().addMinutes(2);
        
        String day = string.valueOf(dtNow.day());
        String month = string.valueOf(dtNow.month());
        String hour =  string.valueOf(dtNow.hour());
        String minute = string.valueOf(dtNow.minute());
        String second = string.valueOf(dtNow.second());
        String year = string.valueOf(dtNow.year());
        
        String strJobName = 'scheduledTesting-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.schedule(strJobName, strSchedule, new scheduledTesting());
    }
    
    private void test_Method(){
        system.debug('>>>>>' + System.Now());
    }
}

All Answers

Keyur  ModiKeyur Modi
Hi,
You can use this below line which will execute your batch for every 5 minute
 
batchClass batchCl= new batchClass ();
String cronStr = '0 0,5 * * * *';
System.schedule('Process Accs Job', cronStr, batchCl);

please let me know if this will help,

Thanks,
Keyur Modi
 
Ketan MahetKetan Mahet
Hi Keyur,
    While running the job I m getting an error:

Line: 19, Column: 1
System.StringException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.

Thanks,
Ketan Mehta
Keyur  ModiKeyur Modi
Hi,

Sorry there is Typo in my last ans,
sch = '0 15 * * * ?'; 
system.schedule('Process Queued Unit Tests At Each Quarter After',sch,atj);

Thanks,
Keyur Modi
 
Ketan MahetKetan Mahet
Hi Keyur,
    The code is not working as expected. If I use ur line of code it is scheduling the job for every 30 mins. In case use my code, after the current hour, it is not rescheduling the job.

global class scheduledTest implements Schedulable{
    global void execute(SchedulableContext SC) {
        test_Method();
        String day = string.valueOf(system.now().day());
        String month = string.valueOf(system.now().month());
        String hour =  string.valueOf(system.now().hour());
        String minute = string.valueOf(system.now().minute());
        String second = string.valueOf(system.now().second());
        String year = string.valueOf(system.now().year());
        
        String strJobName = 'DistJob-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        //String strSchedule = '0 ' + minute + ' * * * ?';  //My code
        String strSchedule = '0 2 * * * ?';
        System.schedule(strJobName, strSchedule, new scheduledTest());
    }
    
    private void test_Method(){
        system.debug('>>>>>' + system.now());
    }
}




 
Ketan MahetKetan Mahet
Hi,
   Finally I found an answer with Caveat: (After 100 schedule job, I will throw exception "You have exceeded the maximum number (100) of Apex scheduled jobs"). I am working on it.

global class scheduledTesting implements Schedulable{
    global void execute(SchedulableContext SC) {
        
        
        test_Method();
        
        DateTime dtNow = System.Now().addMinutes(2);
        
        String day = string.valueOf(dtNow.day());
        String month = string.valueOf(dtNow.month());
        String hour =  string.valueOf(dtNow.hour());
        String minute = string.valueOf(dtNow.minute());
        String second = string.valueOf(dtNow.second());
        String year = string.valueOf(dtNow.year());
        
        String strJobName = 'scheduledTesting-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.schedule(strJobName, strSchedule, new scheduledTesting());
    }
    
    private void test_Method(){
        system.debug('>>>>>' + System.Now());
    }
}
This was selected as the best answer