Created
February 2, 2019 07:58
-
-
Save SpenceDiNicolantonio/c767708a9adf3a31fcc482e6f064faf1 to your computer and use it in GitHub Desktop.
[Scheduled Apex] Example of a schedulable Apex job #salesforce #apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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