Job schedule by using Apex programming:
Job schedule means, we can schedule a job/class for every hour/day/week/month/year by Apex programming in Salesforce.
Schedule class: Scheduler is a class to run the peace code (class) at a specific time, first we have to implement the schedulable interface and then we have to schedule that scheduler class. We have to two ways to schedule a class.
1. Job Schedule from UI
Schedule a class through standard Salesforce user interface.
Create Apex class that implements Salesforce provided interface schedulable.
global class TestSchedulerClass implements Schedulable{ global void execute(SchedulableContext sc){ // Create Account Object with the name test. Account account = new Account; account.name = ‘Test account created from scheduler’; Insert account; } }
Schedule this class with a standard interface.
Go to Setup
->develop
->Apex Classes
->Click on Schedule a class
button and provide the data as like shown in the below screenshot.
This scheduler runs every day at 12 AM and we can check the status in scheduled jobs under the monitor section.
Note: The above standard interface does not work for some scenarios like, if we want to schedule the same class every 15mins or monthly, etc… So, in this scenario, we can go for the system. Schedule method
2. Schedule a class through the System.Schedule method.
We run following Apex script in an anonymous block to schedule the above class to run every 15mins using CRON expression.
System.schedule(' TestSchedulerClass Job1', '0 0 * * * ?', new TestSchedulerClass ()); System.schedule(‘TestSchedulerClass Job 2', '0 15 * * * ?', new TestSchedulerClass ()); System.schedule('TestSchedulerClass Job 3', '0 30 * * * ?', new TestSchedulerClass ()); System.schedule('TestSchedulerClass Job 4', '0 45 * * * ?', new TestSchedulerClass ());