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
Daniel KDaniel K 

Scheduled Apex execution

Hi All,
         I have a scheduled apex job which executes everyday @1 AM.
          
         I want to execute batch apex b4 and b5 only after complete execution of b1,b2 and b3.
         How can I do this?
         
global class schedulebatch implements Schedulable {
   global void execute(SchedulableContext sc) {
   
 
    batchapex1 b1 = new batchapex1();
    Database.executeBatch(b1,200);

    batchapex2 b2 = new batchapex2();
    Database.executeBatch(b2,200);

    batchapex3 b3 = new batchapex3();
    Database.executeBatch(b3,200);

    batchapex4 b4 = new batchapex4();
    Database.executeBatch(b4,200);
    
    batchapex5 b5 = new batchapex5();
    Database.executeBatch(b5,200);    

   }
}

 
Best Answer chosen by Daniel K
Jainam ContractorJainam Contractor
Hi Daniel,

In this case, you can do one thing. You can do Batch chaining. i.e Call B2 from finish method of B1, B3 from finish method of B2, B4 from finish method of B3 and B5 from finish method of B4. So in this case all the jobs will be executed in sequence and you do not need to worry about how much time each job will take as the next job will start only after the execution of the previous job.

All jobs will execute in the following seq: B1 --> B2 --> B3 --> B4 --> B5

Regarding your questions, there is no specific sequence in which all the 3 jobs will be executed if they all are included on the Schedulable class. So rather include only B1 in the Schedulable class and then do the chaining as i have mentioned above. 

Please let me know if it solves your problem and mark the question as solved.

Thanks,
Jainam Contractor.

All Answers

Jainam ContractorJainam Contractor
Hi Daniel,

Rather than calling those batches in this Schedulable Class, if you want to execute these batches in sequence, you can call those batches from the finish method of the previous batches and call only the Parent batch in the Schedulable Apex.

For Eg: If you want to execute Batch b4 after Batch b3, then call Batch b4 in the finish method of the Batch b3 and like wise you can chain those batch classes to execute in sequence.

Let me know if you need more assistance.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Daniel K,

May I suggest you please refer the below link for reference. hope it helps.

please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
Daniel KDaniel K
Hi jainam,
                Among b1,b2,b3 I wouldn't know which one completes first/last as it completely depends on data for that day.

               So, in which job's finish method should I call b4 & b5 ?

                if I have b1,b2 and b3 batch apex jobs scheduled in schedulable apex,
                1) all 3 batch jobs start executing at a time ?
                2) first b1 then b2 and then b3 ?

                If point 2 is true, then I can include b4 and b5 in finish method of b3.
                If point 1 is true, then I can't include in any of b1,b2 or b3 as i can't say which will finish last.

               Hope you got my question.

               Thanks for your response.                   
              
Jainam ContractorJainam Contractor
Hi Daniel,

In this case, you can do one thing. You can do Batch chaining. i.e Call B2 from finish method of B1, B3 from finish method of B2, B4 from finish method of B3 and B5 from finish method of B4. So in this case all the jobs will be executed in sequence and you do not need to worry about how much time each job will take as the next job will start only after the execution of the previous job.

All jobs will execute in the following seq: B1 --> B2 --> B3 --> B4 --> B5

Regarding your questions, there is no specific sequence in which all the 3 jobs will be executed if they all are included on the Schedulable class. So rather include only B1 in the Schedulable class and then do the chaining as i have mentioned above. 

Please let me know if it solves your problem and mark the question as solved.

Thanks,
Jainam Contractor.
This was selected as the best answer
Daniel KDaniel K
Hi Jainam,
                  Thanks for the info and detailed explanation.

                  Actually, in my case I already have a scheduler with b1,b2 and b3 batch classes scheduled.
                  After execution of those, I have to add 6 more batch classes within the same scheduler.
                  So, if I schedule all 9 batch jobs from scheduler, are there any chances that I will be hit with
                  "Attempted to schedule too many concurrent batch jobs in this org"  or will remaining batch apex's will
                   be queued until first 5 batch apexes are completed ?

                  I know you have already answered my initial question. This is just for my understanding.

                  Thanks a lot.
                  
                  
Jainam ContractorJainam Contractor
Hi Daniel,

You won't hit the Concurrent Batch jobs limit even if you have more than 5 jobs been called in a Scheduleable APEX. It takes first 5 jobs and rest are in queue until the other jobs gets executed.

You can schedule 100 jobs at a time with 5 active batch jobs.

Please let me know if it helps you or you need any more assistance.

Thanks,
Jainam Contractor