Example1:
Create a task for the opportunity owners who's opportunity is not closed even close date cross the close date.
Note: Schedule and Batch classes always should be
Global.
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);
}
}
//Code from Salesforce.com - Trailhead
Note:
TaskUtils.remindOwners(opptys); is creates a task for the owners.
Now, Autorun the jobs for every Monday at 2 am.
Schedule jobs run as ->
Seconds Minutes Hours DaysOfMonth Month DayOfWeek Year
There are also some special characters you can use:
, used to delimit values [hours, day of month, month, day of week, year]
– used to specify a range [hours, day of month, month, day of week, year]
* used to specify all values [hours, day of month, month, day of week, year]
? used to specify no specific value [day of month, day of week]
/ used to specify increments [hours, day of month, month, day of week, year]
L used to specify the end of a range [day of month, day of week]
W used to specify the nearest weekday [day of month]
# used to specify the nth day of the month [day of week]
RemindOpptyOwners reminder = new RemindOpptyOwners();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String sch = '0 30 8 10 2 ?';
String jobID = System.schedule('Remind Opp Owners', sch, reminder);
Testing:
http://blog.deadlypenguin.com/blog/2012/05/26/scheduled-actions-in-salesforce-with-apex/
I'll write my own test, Coming soon with real time examples.