0

enter code hereCurrently I have a batch which I'm scheduling after 2-2 minutes but because it has the same job name it doesn't allow to reschedule. Is there any way to reschedule the same batch. Because when i go to All schedule job in UI I can see it listed with no next schedule run. Now If i give different name it will run but with all the schedule batch listed in All schedule job . Please suggest me how to reschedule the same batch by apex.

String day = string.valueOf(system.now().day());
    String month = string.valueOf(system.now().month());
    String year = string.valueOf(system.now().year());
    String minute = string.valueOf(system.now().minute() + 2);
    String hour = string.valueOf(system.now().hour());
    String nextFireTime = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
    BatchSample b = new BatchSample(); 
    CronTrigger[] jobList = [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, CronJobDetail.JobType,NextFireTime 
                                  FROM CronTrigger Where CronJobDetail.Name Like 'BatchSample' AND NextFireTime > :System.now()];
    if(jobList.size()==0)
        System.schedule('BatchSample', nextFireTime, b);
  • Could you please share the cron expression you're using? – blank Aug 21 '18 at 19:14
  • @blank updated the question with code – Ravi Roy Aug 21 '18 at 19:33
  • 3
    This seems like a very XY-ish design to me. Why not use batch or queueable chaining? Why schedule a batch class two minutes in the future with the same constant name? Why not use System.scheduleBatch() with the minutesFromNow parameter? – David Reed Aug 21 '18 at 20:12
0

While scheduling the Job you can put different name all jobs like append current Datetime:

I have put example for this:

Datetime sysTime = System.now().addSeconds( 120 );

String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();

System.schedule( job.getJobName() + sysTime, chronExpression, (System.Schedulable)job );

you can put different name in first parameter of System.schedule

  • Yes If i put different name it will schedule, But I want the same scheduled Apex to get reschedule is that possible ? – Ravi Roy Aug 21 '18 at 18:38
  • 1
    Well it will actually be the same Apex. Just the different job name. If you want to reschedule exactly the same job, you have to wait until the previous one finishes. – blank Aug 21 '18 at 18:45
  • @RaviKant, Are you reschedule your batch when first batch is finished or some other way? – Chandra Kiran Aug 21 '18 at 18:55
  • @ChandraKiran yes I'm trying to give the new date time to the scheduled apex but it says 'The job name is already scheduled for execution' . Also I'm scheduling in the finish method – Ravi Roy Aug 21 '18 at 19:07
  • @blank until and unless I don't delete it from 'Schedule job' page it doesn't allow me to create batch with same name even though the batch has been processed. – Ravi Roy Aug 21 '18 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.