How Can We Deploy Apex Classes That Are Scheduled?

Question
When deploying Apex classes that are scheduled, the main challenge is that Salesforce locks scheduled Apex jobs to the specific class they reference. This can cause deployment failures if you try to update a scheduled class without first unscheduling its jobs.
How can we effectively deploy updates to scheduled Apex classes without running into these issues?
Answer
One way to handle this is to manually abort scheduled jobs before deployment and reschedule them afterward. However, a more flexible approach is to use a dynamic dispatcher pattern, as presented by Dan Appleman in his Dreamforce 2013 session “Design Patterns for Asynchronous Apex.”
Using a Dispatcher Class to Avoid Deployment Issues
The idea is to have a ScheduledDispatcher class that remains locked but delegates execution to a separate ScheduleHandler class using Type.forName()
. This allows us to update the handler class freely while keeping the dispatcher unchanged.
Implementation:
Step 1: Create the Dispatcher Class
global class ScheduledDispatcher implements Schedulable {
public interface IScheduleDispatched {
void execute(SchedulableContext sc);
}
global void execute(SchedulableContext sc) {
Type targetType = Type.forName('ScheduleHandler');
if (targetType != null) {
IScheduleDispatched obj = (IScheduleDispatched) targetType.newInstance();
obj.execute(sc);
}
}
}
Explanation:
The ScheduledDispatcher
implements the Schedulable
interface, which Salesforce requires for scheduled jobs.
It defines an interface IScheduleDispatched
, which enforces an execute()
method.
The execute()
method dynamically loads ScheduleHandler
using Type.forName()
, allowing us to update ScheduleHandler
without modifying ScheduledDispatcher
.
Since ScheduledDispatcher
never changes, it avoids deployment failures.
Step 2: Create the Schedule Handler Class
public class ScheduleHandler implements ScheduledDispatcher.IScheduleDispatched {
public void execute(SchedulableContext sc) {
// Your scheduled logic here
}
}
Explanation:
The ScheduleHandler
class implements the IScheduleDispatched
interface, ensuring it has an execute()
method. This class contains the actual scheduled job logic. Since it is dynamically loaded by ScheduledDispatcher
, we can update it without worrying about deployment issues.
Alternative Approach: Unscheduling Before Deployment
If you don’t want to use the dynamic dispatch pattern, another approach is to unschedule all jobs referencing the Apex class before deployment and reschedule them afterward.
Step 1: Abort the Scheduled Job Before Deployment
List<CronTrigger> jobs = [SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = 'Your_Scheduled_Job_Name'];
for (CronTrigger job : jobs) {
System.abortJob(job.Id);
}
Explanation:
The System.abortJob(job.Id);
call removes them from the scheduler, preventing deployment issues.
This SOQL query retrieves all scheduled jobs matching the given name.
Step 2: Reschedule the Job After Deployment
String cronExp = '0 0 12 * * ?'; // Runs daily at noon
System.schedule('Your_Scheduled_Job_Name', cronExp, new YourScheduledClass());
Explanation:
You must manually run this after deployment.
This schedules YourScheduledClass
to run daily at 12:00 PM using a cron expression.
Which Approach Should You Use?
If your scheduled job logic changes frequently, use the dispatcher pattern. It avoids deployment issues without needing manual intervention.
If your job logic rarely changes, manually unscheduling before deployment might be a simpler option.
The dispatcher pattern provides a scalable, deployment-friendly solution, while unscheduling works well for one-time updates.
Job-Oriented Salesforce Training with 100% Money Back Guarantee
Our Salesforce course is designed to provide an in-depth understanding of the Salesforce platform, equipping you with the essential skills to succeed in the CRM industry. The program covers fundamental modules like Salesforce Admin, Developer, and AI, combining theoretical learning with hands-on experience. Through real-world projects and practical exercises, you’ll develop the expertise to address complex business challenges using Salesforce solutions. Our experienced instructors ensure you gain both technical proficiency and industry knowledge to thrive in the Salesforce ecosystem.
In addition to technical training, our Salesforce Training in Kozhikode offers personalized mentorship, certification support, and interview preparation to enhance your career prospects. You’ll benefit from comprehensive study materials, hands-on project experience, and dedicated guidance throughout the course. By the end of the program, you’ll be fully prepared for certification exams and real-world applications, possessing the problem-solving skills that employers value. Begin your Salesforce journey today and unlock exciting career opportunities. Sign up for a Free Demo now!