2

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)
{
 if(Trigger.isUpdate)
 {
  for(Compliance_Incident_abv__c s:Trigger.New)
  {
     ComplianceIncidents_expireNotify st = new ComplianceIncidents_expireNotify();
     database.executeBatch(st);
  }
 }
}

Please let me know if batch code is also needed.

Thanks!

  • 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 Sep 10 '15 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? – Testing_SFDC Sep 10 '15 at 7:57
1

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 :)

|improve this answer|||||
0

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

|improve this answer|||||

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.