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
elliot catherinelliot catherin 

Schedule the same batch apex class at the same time.

Hi dear community,

I'm trying to schedule a batch apex class several time at the same start time.

I pass a parameter to my apex batch class : 

TBR_APB026_SendSMS batch = new TBR_APB026_SendSMS(partner);

And then the parameter is use to filter the data in my queryLocator :

AND ServiceTerritory.TopLevelTerritory.TBR_Partner__r.Name =: partner

 

However, when I try to schedule several instance of this apex batch class with different values in the parameter, at the same time, with the code bellow : 
 

Set<String> partners = new Set<String>{'PARTNER_A','PARTNER_B','PARTNER_C'};
for(String partner : partners){
    TBR_APB026_SendSMS batch = new TBR_APB026_SendSMS(partner);
    String sch = '0 15 10 14 2 ?';
    system.schedule('TBR_APB026_SendSMS_' + partner, sch, batch);
}

 

After that, only the first one scheduled batch class is executed, and the two others are not event started.

 

Best Answer chosen by elliot catherin
Prateek Prasoon 25Prateek Prasoon 25
One possible reason why only the first scheduled batch class is executed and the others are not even started could be due to the use of the same cron expression for all scheduled jobs.
The cron expression '0 15 10 14 2 ?' used in the code you provided means that the batch class will run every year on February 14th at 10:15 AM. If you schedule multiple jobs with the same cron expression, they will all attempt to run at the same time, which may cause issues with the execution of the batch classes.
To avoid this issue, you can modify the cron expression for each scheduled job so that they are scheduled to run at different times. One approach could be to add a delay between each scheduled job. You can use the System.now().addSeconds() method to add a delay of a few seconds for each job
Set<String> partners = new Set<String>{'PARTNER_A','PARTNER_B','PARTNER_C'};
Datetime startTime = System.now().addSeconds(5); // add a 5-second delay between each scheduled job
for(String partner : partners){
    TBR_APB026_SendSMS batch = new TBR_APB026_SendSMS(partner);
    String sch = startTime.second() + ' ' + startTime.minute() + ' ' + startTime.hour() + ' ' + startTime.day() + ' ' + startTime.month() + ' ? ' + startTime.year();
    system.schedule('TBR_APB026_SendSMS_' + partner, sch, batch);
    startTime = startTime.addSeconds(5); // add 5 seconds to the start time for the next job
}

If you find this answer helpful,Please mark it as  the best answer

All Answers

Shri RajShri Raj

When you schedule several instances of a batch apex class at the same time with different values in the constructor parameter, they will all start at the same time, but the order in which they will be executed is not guaranteed. The system will execute the batches according to the resources available, and it will try to execute them in parallel.
In your case, it seems that the first batch that you scheduled is taking up all the available resources, and the other batches are waiting for the resources to become available. You can check the status of the scheduled jobs to confirm this.
To ensure that the batches are executed in parallel, you can try setting the concurrentJobs parameter to a higher value when you schedule the batches. For example, you can try setting it to 5 to allow up to 5 concurrent jobs to run:
Set<String> partners = new Set<String>{'PARTNER_A','PARTNER_B','PARTNER_C'};
for(String partner : partners){
    TBR_APB026_SendSMS batch = new TBR_APB026_SendSMS(partner);
    String sch = '0 15 10 14 2 ?';
    system.schedule('TBR_APB026_SendSMS_' + partner, sch, batch);
    // Set the concurrentJobs parameter to 5
    System.scheduleBatch(batch, 'TBR_APB026_SendSMS_' + partner, 5);
}


This should allow the batches to be executed in parallel and improve the overall performance of your job.
Prateek Prasoon 25Prateek Prasoon 25
One possible reason why only the first scheduled batch class is executed and the others are not even started could be due to the use of the same cron expression for all scheduled jobs.
The cron expression '0 15 10 14 2 ?' used in the code you provided means that the batch class will run every year on February 14th at 10:15 AM. If you schedule multiple jobs with the same cron expression, they will all attempt to run at the same time, which may cause issues with the execution of the batch classes.
To avoid this issue, you can modify the cron expression for each scheduled job so that they are scheduled to run at different times. One approach could be to add a delay between each scheduled job. You can use the System.now().addSeconds() method to add a delay of a few seconds for each job
Set<String> partners = new Set<String>{'PARTNER_A','PARTNER_B','PARTNER_C'};
Datetime startTime = System.now().addSeconds(5); // add a 5-second delay between each scheduled job
for(String partner : partners){
    TBR_APB026_SendSMS batch = new TBR_APB026_SendSMS(partner);
    String sch = startTime.second() + ' ' + startTime.minute() + ' ' + startTime.hour() + ' ' + startTime.day() + ' ' + startTime.month() + ' ? ' + startTime.year();
    system.schedule('TBR_APB026_SendSMS_' + partner, sch, batch);
    startTime = startTime.addSeconds(5); // add 5 seconds to the start time for the next job
}

If you find this answer helpful,Please mark it as  the best answer
This was selected as the best answer
elliot catherinelliot catherin

Hi Shri Raj,

Thanks for your answer, you said :

"The system will execute the batches according to the resources available, and it will try to execute them in parallel."

Indeed, it is the behavior I observed.

 

However, you told me about modifying a parameter "concurrentJobs" of the System.scheduleBatch method.
I checked the documentation, and I can't find this param : 

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_system.htm#apex_System_System_scheduleBatch

There are only two signatures for this method : 
- scheduleBatch(batchable, jobName, minutesFromNow)
- scheduleBatch(batchable, jobName, minutesFromNow, scopeSize)