With the Spring ’25 release, Salesforce has introduced new methods in the System class that allow developers to pause and resume scheduled jobs programmatically. These enhancements are particularly useful for managing resource-intensive, business-critical processes that may be running at any given time. By leveraging these methods, developers can optimize resource allocation and ensure smooth execution of dependent processes.
Newly Introduced Methods:
System.pauseJobByName(String jobName)System.pauseJobById(Id jobId)System.resumeJobByName(String jobName)System.resumeJobById(Id jobId)
These methods provide greater flexibility in handling scheduled jobs, allowing developers to pause execution when necessary and resume it when conditions permit.
Example: Pausing a Scheduled Job
Consider a scenario where you have a scheduler class named jobSchedulerClass, and you need to pause its execution dynamically from Apex. You can achieve this with the following approach:
//Scheduled Class to be paused
List<ApexClass> apexClassList = [SELECT Id FROM ApexClass WHERE Name = 'jobSchedulerClass'];
if (!apexClassList.isEmpty()) {
List<AsyncApexJob> jobsList = [SELECT CronTriggerId FROM AsyncApexJob WHERE ApexClassId = :apexClassList[0].Id AND Status = 'Queued'];
for (AsyncApexJob jobToPause : jobsList) {
//pause the scheduled job
System.pauseJobById(jobToPause.CronTriggerId);
}
}
Resuming a Paused Job
Resuming a job follows a similar pattern. Once the necessary conditions are met, you can restart the execution using:
//Scheduled Class to be resumed
List<ApexClass> apexClassList = [SELECT Id FROM ApexClass WHERE Name = 'jobSchedulerClass'];
if (!apexClassList.isEmpty()) {
List<AsyncApexJob> jobList = [SELECT CronTriggerId FROM AsyncApexJob WHERE ApexClassId = :apexClassList[0].Id];
for (AsyncApexJob job : jobList) {
//resume the scheduled job
System.resumeJobById(job.CronTriggerId);
}
}
Conclusion
The ability to programmatically pause and resume scheduled jobs in Apex introduces a new level of control and efficiency for developers managing complex automation processes. Whether it’s optimizing resource usage, preventing conflicts, or ensuring smooth execution of dependent workflows, these new methods empower developers with greater flexibility in scheduling job management.
Stay ahead of the curve with Spring ’25 and leverage these enhancements to refine your Salesforce automation strategies!
Learn Moar!
For detailed release notes and additional resources, visit the Salesforce Release Updates page. Find out about SOQL Changes and Error Updates and keep following SalesforceMonday.com for expert tips, tutorials, and the latest news on Salesforce development.
Leave a Reply