function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alejandro RosanoAlejandro Rosano 

How can I check in Apex if a batch is scheduled?

I am calling a batch through a system.scheduleBatch(BatchableClass(query), 'Job Name', MinutesToExecute) method. How can I check if the batch job Job Name is already scheduled?

Thanks in advance.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Alejandro:

 First you need to query the CronJobDetail Object using the name of the job.Below is the link for cronjobdetail object:

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_cronjobdetail.htm


Once you get the id of the desired cronjobdetail, using that id, query the crontrigger obejct and get the nextfiretime., to see if it has any future runs left!, below is the link for crontrigger object

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_crontrigger.htm


Below link will help you framing the queries:

http://docs.releasenotes.salesforce.com/en-us/winter14/release-notes/rn_186_apex_job_name_type.htm


Hope it helps:

Cheers!

Thanks,
Balaji
Pankaj_GanwaniPankaj_Ganwani
Hi Alejandro,

You can check in scheduled job in setup. If you have scheduled the job already then it will be shown there.
Mustafa JhabuawalaMustafa Jhabuawala
List<ApexClass> apList = [Select Id from ApexClass where Name = <BATCHCLASSNAME>'];
            
Integer enqueuedJobs = [SELECT COUNT() FROM AsyncApexJob 
                                    WHERE JobType='BatchApex' 
                                    AND Status IN ('Processing','Preparing','Queued') 
                                    AND ApexClassID =: apList[0].Id] ;
            
if(enqueuedJobs <= 0){
//EXECUTE YOUR BATCH HERE
}
else{
//BATCH IS ALREADY SCHEDULED - SHOW NOTIFICATION
}

This is how you can check whether your batch job is scheduled in APEX.

Note - I know this was posted long back in 2015 but few clarification were missing, thus thought that I should post this to help future trailblazers.

Hope this helps.

--
Thank You
Mustafa