I have a quick question for which I could not find any answer in the salesforce documentation.

Suppose I have a scheduled job which invokes batch job every hour. If it invoked one batch at 10AM, which is modifying records. Now suppose it takes much time and it doesn't finish till 11AM. Will scheduler instantiate second batch at 11AM? or will it wait for the first instance to completed?

Second question: Suppose if salesforce invokes two instances, will both instance will work on same set of records? it might get worst if it does so. I will need a solution in this case to detect and stop batch from working on same set of records.

Thanks, Bhupendra

2 Answers 2


Ideally, in order to make sure that your subsequent batch runs only after the first batch is complete, you should schedule it for one hour later from the finish method of the batch class.This will make sure that no two instances of the same batch class are running concurrently.


Yes Salesforce can schedule 5 batch in parallel so there are chances that you batch might over lap.

You can do something like this

//check if there are 5 active batch jobs
//In some cases, might need to add "Status='Queued' " to the criteria
if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')] < 1){ 
} else {
   //schedule this same schedulable class again in 30 mins
   nameOfYourSchedulableClass sc = new nameOfYourSchedulableClass();
   Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins
   String timeForScheduler = dt.format('s m H d M \'?\' yyyy');
   Id schedId = System.Schedule('MatrixRetry'+timeForScheduler,timeForScheduler,sc);

It will check if any batch in processing then it will schedule your class to run 30 min late. In below query you can add your class Id in filter so it will check for your particular class and if you get records then set your schedule class to run 30 minutes late.


This is the query

[SELECT Id, CreatedDate, CreatedById, JobType, ApexClassId, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors, CompletedDate, MethodName, ExtendedStatus, ParentJobId, LastProcessed, LastProcessedOffset FROM AsyncApexJob WHERE ApexClassId = 'Class Id here']

You can pass your class id in filter

  • Do you know how to prove this fact? I invoked the same batch two times and both went into 'processing' state and started processing . database.executeBatch(new mybatch()); I executed above statement two times. Didn't tried with system.schedule() although.
    – byadav
    Sep 27, 2016 at 7:15
  • @BhupendraYadav yes you are correct here. Check my updated answer. Sep 27, 2016 at 7:27
  • Hi Tushar, This snippet is just checking for the 5 active batches. Is it possible to check if the same batch class is in progress? instead of count?
    – byadav
    Sep 27, 2016 at 9:28
  • @BhupendraYadav yes it is possible. check my updated answer. Sep 27, 2016 at 9:33
  • 1
    Seems Good enough. I will verify it and update here back.
    – byadav
    Sep 27, 2016 at 9:50

You must log in to answer this question.

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