How to Deploy Apex Classes That Are Scheduled?

How to Deploy Apex Classes That Are Scheduled?

On February 15, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on How to Deploy Apex Classes That Are Scheduled?
How to Deploy Apex Classes That Are Scheduled?

Question

How can we manage scheduled jobs in Salesforce to allow updates to the classes they reference? Specifically, is there a way to use Type.forName or other techniques to decouple classes from scheduled jobs and enable seamless deployments of Apex classes? How do we deploy Apex classes that are scheduled?

Answer

When working with scheduled Apex in Salesforce, a common challenge arises when you need to deploy updates to classes that are already scheduled. The system restricts modifications to a class if it is referenced in a scheduled job. This limitation can be problematic during deployments, especially when updates are essential. But how can you deploy Apex classes that are scheduled?

CRS Info Solutions provides leading Salesforce Online training, featuring real-time projects and certification guidance. They offer interview coaching to ensure job readiness.

One effective solution to this issue, as presented by Dan Appleman during his Dreamforce 2013 session “Design Patterns for Asynchronous Apex,” is to decouple the scheduled job from the actual handler logic using a dynamic dispatcher. This approach leverages the Type.forName method to dynamically load the handler class, allowing you to update the handler logic without affecting the scheduled job. Here’s how it works:

First, you create a ScheduledDispatcher class that implements the Schedulable interface. This class serves as a wrapper or dispatcher for the actual logic involved in deploying classes that are scheduled.

Code example on How to Deploy Apex Classes

global class ScheduledDispatcher implements Schedulable {
    public interface IScheduleDispatched {
        void execute(SchedulableContext sc);
    }

    global void execute(SchedulableContext sc) {
        // Dynamically load the handler class
        Type targetType = Type.forName('ScheduleHandler');
        if (targetType != null) {
            IScheduleDispatched obj = (IScheduleDispatched) targetType.newInstance();
            obj.execute(sc);
        }
    }
}

Code Explanation on How to Deploy Apex Classes:

The ScheduledDispatcher class implements the Schedulable interface, which makes it schedulable. It defines an inner interface IScheduleDispatched to enforce a contract for handler logic. The execute method uses Type.forName to dynamically load a handler class (ScheduleHandler) by its name. The handler is instantiated, and its execute method is called, enabling decoupling from the scheduled job.

Next, you define the ScheduleHandler class, which contains the actual business logic. This class implements the IScheduleDispatched interface.

public class ScheduleHandler implements ScheduledDispatcher.IScheduleDispatched {
    public void execute(SchedulableContext sc) {
        // Add your business logic here
        System.debug('Executing scheduled handler logic.');
    }
}

Code Explanation:
The ScheduleHandler class implements the IScheduleDispatched interface defined in ScheduledDispatcher. This ensures that it conforms to the expected structure. The execute method contains the business logic that will run when the job is triggered. In this example, it simply logs a message, but it can be extended with more complex logic.

With this pattern, the ScheduledDispatcher class is the one that gets scheduled, while the actual logic resides in the ScheduleHandler class. Because the ScheduledDispatcher uses dynamic loading via Type.forName, you can freely update the ScheduleHandler class without encountering deployment issues related to scheduled jobs.

If you need to deploy changes to the ScheduleHandler, simply deactivate the current scheduled job, make your updates, and then reschedule the job using the ScheduledDispatcher. This approach ensures minimal disruption and allows for a more flexible deployment process. Indeed, deploying Apex classes that are scheduled can be managed smoothly this way.

Alternatively, if you do not want to use dynamic loading, you can programmatically unschedule the job, deploy the updated class, and then reschedule the job. However, this requires additional steps and does not provide the same level of flexibility as the dynamic dispatcher pattern.

Boost Your Career with Salesforce Training in Singapore

Our Salesforce online training offers an in-depth and immersive learning experience, equipping you with the essential skills to thrive in the CRM industry. The program covers key areas like Salesforce Admin, Developer, and AI, blending strong theoretical knowledge with hands-on experience. Through live projects and assignments, you’ll develop the expertise to tackle complex business challenges using Salesforce solutions. Our skilled instructors are dedicated to helping you gain both technical expertise and valuable industry insights.

Along with technical training, our Salesforce Training in Singapore provides personalized mentorship, exam preparation support, and interview coaching to help you excel in the competitive job market. You’ll have access to comprehensive study resources, real-world projects, and continuous assistance throughout your journey. By the end of the course, you’ll be ready for certification exams and equipped with the practical skills that employers demand.

Don’t hesitate—join our free demo class today and take the first step toward a successful future! Enroll now for a free demo..!!

Comments are closed.