21

I am looking for a solution where I can schedule more than 5 jobs (Running/Queued) at a time. but there is a governor limit that, we can have only up to 5 jobs that can be in Running/Queued status at a time.

Can anybody, help me with a workaround to set up a 6th job in Apex scheduler in Salesforce.

1
  • 1
    Are these common jobs that need to run on a regular basis? Perhaps you could have a dispatcher job called that invokes the others? – Mike Chale Oct 4 '13 at 15:20
13

We can have 100 scheduled Apex jobs at one time.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

Things to consider:

  1. Scheduled APEX are like Planes. They can be on-time or little late depending on Queue.
  2. Scheduled Jobs execute as Synchronous Jobs, meaning it would get limits which are respective to Synchronous Jobs(6MB heap size for Synchronous Job or 12 MB heap size for Asynchronous Job).
6
  • 5
    This is a new feature limit in v29.0. So not everyone can use it, yet. The old limit of 5 still applies until up to October 12th. – sfdcfox Oct 4 '13 at 15:22
  • 4
    One point of clarification, scheduled jobs and concurrent batches are different. The limit of concurrent batches is 5 and that limit is not changing in the Winter '14 release. – greenstork Oct 4 '13 at 15:50
  • greenstork is correct. I think Mike Chale is right that if you need to have more than 5 concurrent batches at once, then a dispatcher is the way to go – Chris Duncombe Oct 4 '13 at 15:58
  • 4
    Another option to consider is chaining batches, which you can now do - so if you need to run a series of batches, you can have batch 1 call batch 2 etc... – BritishBoyinDC Oct 4 '13 at 16:19
  • Dispatchers and chains are definitely the way to go, and they're easy to accomplish. – sfdcfox Oct 4 '13 at 18:20
8

One solution that was mentioned in the comments is to call another Batch class from the finish method of another Batch class. This is actually somewhat new functionality (I want to say Spring '13, but not completely sure), but I have used this in quite a few batch classes and it works great. See a sample below

In my Scheduled Class

global void execute(SchedulableContext sc) {

    BatchClass1 Batch1 = new BatchClass1();
    ID batchprocessid = Database.executeBatch(Batch1 ,50);          
}

My Batch1 Batch Class

global class BatchClass1 implements Database.Batchable<sObject>{

    global final string query;

    global BatchClass1(){
        query = 'Select Id, Name From MyObject__c';     
    } 

    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC,List<sObject> scope){
        List<MyObject__c> myObjectsToUpdate = new List<MyObject__c>();

        for(Sobject s : scope){
            MyObject__c o = (MyObject__c) s;
            // Some Logic and/or actions on your objects
        }  

        try{
            update myObjectsToUpdate ;
        }catch (Exception e) {  
            //Your Error trapping   
        }
    }

    /*
    In your finish method, you call your next batch class.  You can chain
    these batch classes as many times as you like.  In the Batch2 finish method,
    can call Batch3 and so on.
    */
    global void finish(Database.BatchableContext BC){
        BatchClass2 Batch2 = new BatchClass2();
        ID batchprocessid = Database.executeBatch(Batch2 ,50);  
    }
}
1

One solution to this which I haven't done yet, but want to play with is implementing code for this application this guy wrote called Relax:

Relax

Taking all the pain out of Force.com Batch and Scheduled Job management.


What Relax lets you do:

  • Run multiple batch/scheduled Apex jobs as often as every 1 MINUTE, every day
  • Mass activate, unschedule, and change ALL of your Scheduled and Batch Apex jobs at once --- minimizing the hassle of code deployments involving Scheduled Apex
  • Mix-and-match your Batch Apex routines into "chains" of jobs that run sequentially on a scheduled basis, without hard-coding the sequences into your code
  • Define string/JSON parameters to pass into Relax jobs you create, allowing for massive reuse of your Batch/Scheduled Apex.
  • Bonus: powerful 'MassUpdate' and 'MassDelete' Apex Classes that can be run as Relax Jobs are pre-included! Never write another line of Scheduled Apex just to do mass-update a million records!

For an intro to Relax, check out this blog post.

0
1

Build a batch job framework:

  1. All of the batch jobs will be added into a custom object
  2. One scheduled job running @ 1 hr intervals looks for records for that particular hour and schedule them based on # of concurrent jobs until all of the batch jobs for that hour are initiated
  3. A flag on this custom object can have run hourly to make sure to initiate hourly. Thus avoiding multiple record entries.

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.