I have below trigger where I am executing a batch apex. I need to see whether the batch job is currently not running. Can someone help me here?

Apex trigger:

trigger BatchTrigger on Compliance_Incident_abv__c (after update)
  for(Compliance_Incident_abv__c s:Trigger.New)
     ComplianceIncidents_expireNotify st = new ComplianceIncidents_expireNotify();

Please let me know if batch code is also needed.


  • You can set a debug log on your name and see that when the trigger is fired then does it execute the batch job or not. Also you can go to "Batch Job" and see if there is any active batch job running.
    – Ruchi
    Commented Sep 10, 2015 at 7:55
  • No I need to execute database.executebatch only if it is not having it's another instance running. Need to query on Async Batch jobs. But simply don't know what status check required. Can someone please help? Commented Sep 10, 2015 at 7:57

3 Answers 3


I haven't done this personally but here are some thoughts.

As you may already know, there's AsyncApexJob that holds instance's background jobs (finished or otherwise).

So I think, you can do a simple SOQL when your batch apex starts, i.e.:

public Database.QueryLocator start(Database.BatchableContext bc) {
    Id thisClassId = '<id_of_you_batch_apex_class>'; //can be in a custom label for easier maintenance.
    Id thisJobId = bc.getJobId();
    AsyncApexJob[] jobs = [select id from AsyncApexJob where id!=:thisJobId AND status='Processing' AND ApexClassId=:thisClassId AND JobType='BatchApex'];
    if (jobs==null || jobs.size()==0) {
        return null;
    else {
         //return records to process here

These are all based on the documentation of AsyncApexJob object.

Basically, the SOQL is trying to get existing batch jobs (JobType='BatchApex') that is currently in processing status (status='Processing') using the current batch apex class (ApexClassId=:thisClassId) and is NOT the currently running job (id!=:thisJobId).

Again, I haven't tried this personally, but this could be it.

Good luck :)


Improving on pnoytechie's answer above:

  1. You can use class name instead of Id
  2. Best to not to use JobType always as execute in progress return a different jobTYpe
  3. Best to return some dummy query that returns 0 records as SF you will see exceptions if not.
    global Database.QueryLocator start(Database.BatchableContext bc)
        Id thisJobId = bc.getJobId();
        List<AsyncApexJob> lstJobs = [SELECT Id FROM  AsyncApexJob
                             WHERE status ='Processing' AND ApexClass.Name = 'Your class name'AND id!=:thisJobId];

            return Database.getQueryLocator([Some query that returns 0 records]);
        return Database.getQueryLocator([working query]);

Normally, I look at Monitoring- >Jobs-> Apex Jobs to check weather a batch is running or not. You can use search menu for it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .