Skip to content

Instantly share code, notes, and snippets.

@SpenceDiNicolantonio
Created February 2, 2019 07:58
Show Gist options
  • Save SpenceDiNicolantonio/c767708a9adf3a31fcc482e6f064faf1 to your computer and use it in GitHub Desktop.
Save SpenceDiNicolantonio/c767708a9adf3a31fcc482e6f064faf1 to your computer and use it in GitHub Desktop.
[Scheduled Apex] Example of a schedulable Apex job #salesforce #apex
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext context) {
List<Lead> leads = [Select Id, LeadSource from Lead where LeadSource = null limit 200];
for (Lead currentLead : leads) {
currentLead.LeadSource = 'Dreamforce';
}
update leads;
}
}
@IsTest
public class DailyLeadProcessorTest {
@IsTest
static void testLeadProcessor() {
final Integer recordCount = 200;
final String cronExpression = '0 0 0 15 3 ? 2022';
// Insert Lead records
List<Lead> leads = new List<Lead>();
for (Integer i = 0; i < recordCount; i++) {
Lead newLead = new Lead(
FirstName = 'Test',
LastName = 'Lead',
Company = 'Company'
);
leads.add(newLead);
}
insert leads;
// Run scheduled job
Test.startTest();
Id jobId = System.schedule('DailyLeadProcessor', cronExpression, new DailyLeadProcessor());
Test.stopTest();
// Verify result
leads = [select Id, LeadSource from Lead];
for (Lead currentLead : leads) {
System.assertEquals('Dreamforce', currentLead.LeadSource);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment