Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active October 5, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcon/2790340 to your computer and use it in GitHub Desktop.
Save pcon/2790340 to your computer and use it in GitHub Desktop.
Scheduled Apex
/**
* To schedule the monthly reconciliation:
* NOTE: It should run at midnight on the first of every month on it's own, but if you make
* changes and need to requeue run the command below from the developer's console
*
* scheduledMonthly.scheduleIt();
*/
global class scheduledMonthly implements Schedulable {
public static String CRON_EXP = '0 0 0 1 * ? *';
/**
* Static method used to schedule the default reconciliation
*
* @return The jobId from the scheduled run
*/
global static String scheduleIt() {
scheduledMonthly sm = new scheduledMonthly();
return System.schedule('Monthly Reconciliation', CRON_EXP, sm);
}
/**
* Builds up all of the new Monthly Reconciliations and Distributions
*
* @param sc The schedulable context
*/
global void execute(SchedulableContext sc) {
RecordType rt = [
select Id
from RecordType
where DeveloperName = 'Recipient'
];
List<MyObject__c> objectList = new List<MyObject__c>();
//Get all of the accounts of type 'Recipient'
for (Account account: [
select Id
from Account
where RecordTypeId = :rt.Id
]) {
objectList.add(new MyObject__c(
Account__c = account.Id
));
}
if (!objectList.isEmpty()) {
insert objectList;
}
}
}
global class scheduledMonthly implements Schedulable {
/**
* Builds up all of the new Objects
*
* @param sc The schedulable context
*/
global void execute(SchedulableContext sc) {
//Code goes here
}
}
global class scheduledMonthly implements Schedulable {
/**
* Builds up all of the new Objects
*
* @param sc The schedulable context
*/
global void execute(SchedulableContext sc) {
RecordType rt = [
select Id
from RecordType
where DeveloperName = 'Recipient'
];
List<MyObject__c> objectList = new List<MyObject__c>();
//Get all of the accounts of type 'Recipient'
for (Account account: [
select Id
from Account
where RecordTypeId = :rt.Id
]) {
objectList.add(new MyObject__c(
Account__c = account.Id
));
}
if (!objectList.isEmpty()) {
insert objectList;
}
}
}
@isTest
class scheduledMonthlyTest {
public static RecordType fetchRecordType(String name) {
return [
select Id
from RecordType
where DeveloperName = :name
];
}
public static Account getAccount(RecordType rt) {
return getAccount(rt, '_unittest_account_: 001');
}
public static Account getAccount(RecordType rt, String name) {
return new Account(
Name = name,
RecordTypeId = rt.Id
);
}
public static CronTrigger fetchCronTrigger(String jobId) {
return [
select CronExpression,
TimesTriggered,
NextFireTime
from CronTrigger
where Id = :jobId
];
}
public static Map<Id, List<MyObject__c>> fetchMyObjects(List<Account> accts) {
Map<Id, List<MyObject__c>> result = new Map<Id, List<MyObject__c>>();
for (Account a: accts) {
result.put(a.Id, new List<MyObject__c>());
}
for (MyObject__c mo: [
select Account__c
from MyObject__c
where Account__c in :result.keySet()
]) {
result.get(mo.Account__c).add(mo);
}
}
static testMethod void testScheduledMonthly() {
RecordType rt = fetchRecordType('Recipient');
Account testAccount = getAccount(rt);
insert testAccount;
Test.startTest();
String jobId = System.schedule(
'_unittest_scheduled_: 001',
scheduledMonthly.CRON_EXP,
new scheduledMonthly()
);
CronTrigger ct = fetchCronTrigger(jobId);
System.assertEquals(scheduledMonthly.CRON_EXP, ct.CronExpression, 'Did not get the same Cron Expression back');
System.assertEquals(0, ct.TimesTriggered, 'The job has been run and should not have');
DateTime today = DateTime.now();
String dateString = ''+today.year()+'-'+today.addMonths(1).month()+'-01 00:00:00';
System.assertEquals(String.valueOf(DateTime.valueOf(dateString)), String.valueOf(ct.NextFireTime), 'Did not get the right fire date');
List<MyObject__c> myObjs = fetchMyObjects(new List<Account>{testAccount}).get(testAccount.Id);
System.assert(myObjs.isEmpty(), 'Should have gotten no objects back');
Test.stopTest();
myObjs = fetchMyObj(new List<Account>{testAccount}).get(testAccount.Id);
System.assert(1, myObjs.size(), 'Did not get the right number of objects back');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment