Salesforce Apex Scheduler

All you Need to Know About Salesforce Apex Scheduler | The Ultimate Guide

Apex Scheduler

Apex code that is scheduled to run at a specific time over a set period of time. A class that repeats at regular intervals is called a schedule apex in Salesforce. We need to implement the interface Schedulable in order to schedule an apex class. When you have to redeploy or alter this code, you can utilise it to schedule it to execute on the first of each month or on a certain day. Apex's scheduler operates in system mode, allowing users to execute classes whether or not they are authorised to do so. Starting a career as a Salesforce Developer is incredibly intriguing because you can browse a new planned apex job every ten minutes. Using the appropriate code and technique, you can also access the scheduled batch apex from the developer console. 

When utilising the Schedule Apex tab in the Salesforce user interface or the System.schedule method, you must first implement the Schedulable interface for the class in order to launch Apex classes to execute at a certain time 

Implementing the Schedulable Interface

Write an Apex class that implements the Schedulable interface supplied by Salesforce first if you want to schedule an Apex class to run periodically. 

Whether or not the user has the authorization to execute a class, the scheduler runs as a system and all classes are executed. 

Enter Scheduled Jobs in the Quick Find box in Setup, then choose Scheduled Jobs to monitor or halt the execution of a scheduled Apex job using the Salesforce user interface.  

The Schedulable interface contains one method that must be implemented, and execute. 

global void execute(SchedulableContext sc){} 

The implemented method must be declared as global or public. 

Use this method to instantiate the class you want to schedule. 

dont miss out iconDon't forget to check out: Apex Triggers in Salesforce | Here's the Ultimate Salesforce Guide

The following example implements the Schedulable interface for a class called mergeNumbers: 

global class scheduledMerge implements Schedulable { 
    global void execute(SchedulableContext SC) { 
        mergeNumbers M = new mergeNumbers();  
    } 
}

To implement the class, execute this example in the Developer Console. 

scheduledMerge m = new scheduledMerge(); 
String sch = '20 30 8 10 2 ?'; 
String jobID = system.schedule('Merge Job', sch, m);

You can also use the Schedulable interface with batch Apex classes. The following example implements the Schedulable interface for a batch Apex class called batchable: 

global class scheduledBatchable implements Schedulable { 
    global void execute(SchedulableContext sc) { 
        batchable b = new catchable();  
        database.executebatch(b); 
     } 
}

An easier way to schedule a batch job is to call the System.scheduleBatch method without having to implement the Schedulable interface. 

Use the SchedulableContext object to keep track of the scheduled job when it's scheduled. The SchedulableContext getTriggerID method returns the ID of the CronTrigger object associated with this scheduled job as a string. You can query CronTrigger to track the progress of the scheduled job. 

To stop the execution of a job that was scheduled, use the System.abortJob method with the ID returned by the getTriggerID method. 

Apex Scheduler Limits

  • There is a cap of 100 planned Apex jobs per user. By visiting the Scheduled Jobs page in Salesforce and generating a custom view with a type filter similar to "Scheduled Apex," you can determine your current count. To obtain the number of Apex scheduled jobs, you can also programmatically query the CronTrigger and CronJobDetail classes.  
  • The most scheduled Apex executions per day are 250,000 or 200 times the number of user licences in your business, whichever is higher. All asynchronous Apex, including Batch Apex, Queueable Apex, Scheduled Apex, and Future Methods, are subject to this limit, which applies to your entire organisation. Make a request to the REST API limits resource to find out how many available asynchronous Apex executions there are. The REST API Developer Guide contains a section on List Organization Limits. Full Salesforce and Salesforce Platform user licences, App Subscription user licences, Chatter Only users, Identity users, and Company Communities users are among the licencing types that count against this cap. 

dont miss out iconCheck out an amazing Salesforce video tutorial here: Introduction To Salesforce Apex | DataType | Collection | Conditional Statements

Apex Scheduler Notes and Best Practices

  • Salesforce arranges for the class to run at the designated time. Depending on the availability of the service, actual execution may be postponed. 
  • If you intend to book a class from a trigger, exercise extreme caution. The trigger can't add more scheduled classes than the limit, therefore you must be able to assure that.
  •  Take into account API bulk updates, import wizards, user interface mass record changes, and any other situations where multiple records can be modified simultaneously. 
  • Although more processing can be done in the execute method, we advise that all processing be done in a different class.
  • Scheduled Apex does not handle synchronous Web service callouts. Make asynchronous callouts by implementing Queueable Apex in your code. interface for the AllowsCallouts marker. if the batch job being run by your scheduled Apex uses the database. Callouts are supported by the batch class via the AllowsCallouts marker interface. Read about using Batch Apex. 
  • When a Salesforce service is offline for maintenance, any Apex jobs that were scheduled to run during that period will now run when system resources become available after the service has resumed. When the service resumes, all scheduled Apex jobs that were already in progress are rolled back and scheduled again. Due to surges in system utilisation following significant service upgrades, planned Apex jobs may start with a longer delay than usual. 
  • From initialization through successive scheduled runs, scheduled job objects, along with the member variables and properties, remain unchanged. In subsequent job executions, the object state from the moment System.schedule() was called is preserved. 
  • Using Database.stateful in Batch Apex, it is feasible to force a new serialised state for fresh jobs. Use the temporary keyword with Scheduled Apex to prevent member variables and properties from being persistent. Read about using the transitory keyword. 

 

Responses

Popular Salesforce Blogs