global class TestSchedulableClass implements Schedulable
{
global void execute(SchedulableContext ctx)
{
// add your logic here
//schedule next execution after a minute
DateTime currentDateTime=System.now().addMinutes(1);
String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
+String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
+String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
TestSchedulableClass testObj = new TestSchedulableClass();
system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, testObj);
for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100])
{
System.abortJob(cronTriggerItem.id);
}
}
}
global class TestSchedulableClass implements Schedulable
{
global void execute(SchedulableContext ctx)
{
// add your logic here
//schedule next execution after a minute
DateTime currentDateTime=System.now().addMinutes(1);
String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
+String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
+String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
TestSchedulableClass testObj = new TestSchedulableClass();
system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, testObj);
for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where
NextFireTime= null AND State='DELETED' Limit 100])
{
System.abortJob(cronTriggerItem.id);
}
}
}
Hi Imo, You can schedule the job in every two minutes through the following code: String scheduleTime = '0 0/2 * 1/1 * ? *'; BatchClassName batchInstance = new BatchClassName(); system.schedule('Schedule Every 2 min', scheduleTime, batchInstance); I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too. Thanks, Ajay Dubedi
@Nagendra, this batch is running every half an hour. @Ajay K Dubedi this is giving me error: System.StringException: Seconds and minutes must be specified as integers: 0 0/2 * 1/1 * ? *
Please find the sample code below. Please try below code to execute. Hope this helps.
Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra
All Answers
Please find the sample code below. Please try below code to execute. Hope this helps.
Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra
You can schedule the job in every two minutes through the following code:
String scheduleTime = '0 0/2 * 1/1 * ? *';
BatchClassName batchInstance = new BatchClassName();
system.schedule('Schedule Every 2 min', scheduleTime, batchInstance);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
You have to schedule your batch for 60 times if you want to run a batch for every 1 minute.
Like this:-
for(Integer i = 0 ; i < 60 ; i++){
System.schedule('Scheduled Job '+i , '0'+i+' * * * ?', new scheduledTest());
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
@Ajay K Dubedi this is giving me error: System.StringException: Seconds and minutes must be specified as integers: 0 0/2 * 1/1 * ? *
Try the following code, it works for me:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi