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
Nimisha PrashantNimisha Prashant 

How to Schedule an apex job after 30 minutes from now to execute a batch at 3pm everyday?

Batch Apex can have 5 concurrent (simultaneous) jobs running in parallel. When I schedule my job I am checking if 5 jobs are already running if yes I want to try scheduling the job again after 30 minutes. The job should still run only at 3pm daily.
Please help me with the apex code for the same as I am new to Salesforce.
 
Balaji BondarBalaji Bondar
Hi Nimisha,

Find the code as below :
//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')] < 5){ 
   Database.executeBatch(batchClassInstance);
} 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);
}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Nimisha PrashantNimisha Prashant
Hi Balaji,
The code schedules the job to run after 30 min from now. But I want to schedule it to run for cron String '0 0 3 ? * MON-SUN' after 30 min from now. Rephrasing my question where do I give my cron String while Scheduling it after 30 minutes from now?
Balaji BondarBalaji Bondar
Namisha, I am sure about the implementation exactly but  I hope below code will help:
ProcessAccs pa= new ProcessAccs();
String cronStr = '0 0,30 * * * *';
System.schedule('Process Accs Job', cronStr, pa);
Arav SundarArav Sundar
Hi Balaji , your is not helping me gettin an error as invalid type ProcessAccs