+ Start a Discussion
Sagar Hinsu 13Sagar Hinsu 13 

how to restrict Schedule Apex job to schedule twice

I have a scenario where I am scheduling a job throw apex class. 
Datetime sysTime = System.now().addMinutes(5);    
String SchTimer = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
system.schedule('scheduleDataJob_'+ SchTimer , SchTimer, new DataScheduleJob(sr));

I want to check whether this job is already scheduled or not? if it is not schedule then it should schedule and if it is scheduled then don't schedule it. 
Aslam ChaudharyAslam Chaudhary
Use below query 
Select c.Name, c.JobType, c.Id From CronJobDetail c

Shilpa KambleShilpa Kamble
Query on existing scheduled jobs i.e. cronJob
Check the job with specific name, if you know the name of scheuled job
List<CronJobDetail > lstJobs = [Select Name, JobType, Id From CronJobDetail where name= 'scheduled job name' limit 1];
             //1. Job is already scheduled,So no need to Schedule it
             //2. abort the scheduled job and then schedule the new one
          // No job is scheduled.
          // Schedule your job

Hope it helps!