You need to sign in to do that
Don't have an account?
m 10
is it possible to run batch every minute
Hi Team ,
Could you please help me below apex batch job need to run every minute
but no lock it's executng every 1hour please find below screenshot
global class ServiceClaimBatchimplements Database.Batchable<SObject>, Database.AllowsCallouts, Database.Stateful{
private void rescheduleBurstSMSResponse(){
try{
Datetime dt = system.now().addMinutes(1);
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 cronExpression = '' + dt.second() + ' ' + dt.minute() + ' ' + dt.hour() + ' ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();
String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
if(Test.isRunningTest()) {
//This will throw Divide by 0 error and code will move to catch block
Integer divideError = 123/0;
}
System.schedule(cronExpression, strSchedule, new ScheduleServiceClaim());
}catch(Exception ex){
System.debug(ex);
System.debug(ex.getMessage());
System.debug(ex.getLineNumber());
}
}
...........................................................
global class ScheduleServiceClaim implements Schedulable {
global void execute(SchedulableContext sc){
Database.executeBatch(new ServiceClaimBatch());
}
}
Will appricate as best answer please help me it is killing my head
Could you please help me below apex batch job need to run every minute
but no lock it's executng every 1hour please find below screenshot
global class ServiceClaimBatchimplements Database.Batchable<SObject>, Database.AllowsCallouts, Database.Stateful{
private void rescheduleBurstSMSResponse(){
try{
Datetime dt = system.now().addMinutes(1);
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 cronExpression = '' + dt.second() + ' ' + dt.minute() + ' ' + dt.hour() + ' ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();
String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
if(Test.isRunningTest()) {
//This will throw Divide by 0 error and code will move to catch block
Integer divideError = 123/0;
}
System.schedule(cronExpression, strSchedule, new ScheduleServiceClaim());
}catch(Exception ex){
System.debug(ex);
System.debug(ex.getMessage());
System.debug(ex.getLineNumber());
}
}
...........................................................
global class ScheduleServiceClaim implements Schedulable {
global void execute(SchedulableContext sc){
Database.executeBatch(new ServiceClaimBatch());
}
}
Will appricate as best answer please help me it is killing my head
System.schedule('Scheduled Job 1', '0 1 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 2', '0 2 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 3', '0 3 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 4', '0 4 * * * ?', new scheduledTest());
I would suggest you to keep longer gap since executing a job might take more than a minute so it's like previous job is not over and new one is started. It might case locking issues.
http://resources.docs.salesforce.com/198/10/en-us/sfdc/pdf/salesforce_app_limits_cheatsheet.pdf
Salesforce does not allow to schedule bach class every minute. But you can use trick to schedule it for every minute.
Use a function and run a for loop to schedule the class for every minute for 1 hr.
Example :
Like schedule 4.05 ---> after one hr it will run at 5.05.
then for 4.06 ---> it will run again at 5.06.
for 4.07 ---> will again run at 5.07.
This is how schedule it for 60 times for every minute. Every minute will execute after 1 hr which is allowed.
Here is the function and CRON EXPRESSION that runs every minutes
If it helped you. support this answer.