You need to sign in to do that
Don't have an account?
Ketan 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);
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);
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
You can use this below line which will execute your batch for every 5 minute
please let me know if this will help,
Thanks,
Keyur Modi
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
Sorry there is Typo in my last ans,
Thanks,
Keyur Modi
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());
}
}
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());
}
}