Play Games

Search This Blog

Monday, December 2, 2019

Trigger must be associated with a job detail error while scheduling the batch job

Trigger must be associated with a job detail error while scheduling the batch job

Problem: This error occurred while scheduling the batch job using cron expression.

System.schedule('Schedule Batch Job that runs only once after 5 mins  ', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());

Solution: Notice in the above sample code, there is space after the name of the job i.e Schedule Batch Job that runs only once after 5 mins.

When I remove extra spaces,I was able to resolve the issue.

System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());

How to schedule a batch job to run only once after 5 mins

How to schedule a batch job to run only once after 5 mins

Assume AccountUpdatorBatchSchedular is the schedular class that schedules the batch job.

global class AccountUpdatorBatchSchedular implements schedulable {
    global void execute(SchedulableContext sc) {
        AccountUpdatorBatch b = new AccountUpdatorBatch();
        database.executebatch(b);
    }
}

AccountUpdatorBatch is the actual batch job.


Sample Code to schedule job:

// Add 5 minutes to current Time
DateTime dtCurrentTime = System.now().addminutes(5);

String sHour = '', sMinute='', sDayOfMonth='', sMonth='', sYear='';

sMinute = String.ValueOf(dtCurrentTime.minute());
sHour = String.ValueOf(dtCurrentTime.hour());
sDayOfMonth = String.ValueOf(dtCurrentTime.day());
sMonth = String.ValueOf(dtCurrentTime.month());
sYear = String.ValueOf(dtCurrentTime.year());   

System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());


Output: