What is Scheduled Apex?
Scheduled apex is all about to run a piece of apex code at some particular time within a period of time. Schedule apex in Salesforce is a class that runs at a regular interval of time. To schedule an apex class, we need to implement an interface Schedulable.
Sample Code
This class queries for open opportunities that should have closed by the current date and creates a task on each one to remind the owner to update the opportunity.
global class RemindOpptyOwners implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Opportunity> opptys = [SELECT Id, Name, OwnerId, CloseDate
FROM Opportunity
WHERE IsClosed = False AND
CloseDate < TODAY];
// Create a task for each opportunity in the list
TaskUtils.remindOwners(opptys);
}
}
You can schedule your class to run either programmatically or from the Apex Scheduler UI.
Excuttion:
RemindOpptyOwners reminder = new RemindOpptyOwners();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String sch = '20 30 8 10 2 ?';
String jobID = System.schedule('Remind Opp Owners', sch, reminder);
Interview quetions::
Q, How schedule a class in every 10 Min.??
System.schedule('Scheduled Job 1', '0 0 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 2', '0 10 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 3', '0 20 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 4', '0 30 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 5', '0 40 * * * ?', new scheduledTest());
System.schedule('Scheduled Job 6', '0 50 * * * ?', new scheduledTest());