In Schedule class run next job from configuration level
global class Schedularclassname implements Schedulable {
global void execute(SchedulableContext SC) {
List<AsyncApexJob> jobs = new List<AsyncApexJob>();
Id thisClassId = [SELECT Id, Name FROM ApexClass WHERE name = 'BatchClassName' LIMIT 1].Id;
//Setup Custom Setting Schedular_Settings__c store the time and no of record process in batach class
//Scheduled_Time__c store time when next batch will run
//Schedular_Records__c add batch size how many record will process
Schedular_Settings__c objschedule = Schedular_Settings__c.getInstance('Schedular');
if(objschedule!=null && objschedule.Scheduled_Time__c != null && objschedule.Schedular_Records__c != null){
jobs = [select id from AsyncApexJob where (status = 'Processing' OR status = 'Holding' OR status = 'Queued' OR status = 'Preparing') AND ApexClassId = :thisClassId AND JobType = 'BatchApex'];
if(jobs == null || jobs.size() == 0){
BatchClassName objBatch = new BatchClassName();
ID batchprocessid = Database.executeBatch(objBatch,Integer.valueOf(objschedule.Schedular_Records__c));}
DateTime nextRun = system.now().addMinutes(Integer.valueOf(objschedule.Scheduled_Time__c));
String day = string.valueOf(nextRun.day());
String month = string.valueOf(nextRun.month());
String hour = string.valueOf(nextRun.hour());
String minute = string.valueOf(nextRun.minute());
String second = string.valueOf(nextRun.second());
String year = string.valueOf(nextRun.year());
String strJobName = 'XYZ JOB Name-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
System.schedule(strJobName, strSchedule, new Schedularclassname());
}}}
Comments
Post a Comment