You need to sign in to do that
Don't have an account?

Create an Apex class that uses Scheduled Apex to update Lead records.-Trail Head Challenge
Here is the Question
Create an Apex class that implements the Schedulable interface to update Lead records with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class. This is very similar to what you did for Batch Apex.
Create an Apex class called 'DailyLeadProcessor' that uses the Schedulable interface.
The execute method must find the first 200 Leads with a blank LeadSource field and update them with the LeadSource value of 'Dreamforce'.
Create an Apex test class called 'DailyLeadProcessorTest'.
In the test class, insert 200 Lead records, schedule the DailyLeadProcessor class to run and test that all Lead records were updated correctly.
The unit tests must cover all lines of code included in the DailyLeadProcessor class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
Here is my code so far
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
list<leads>Lead = [select leadSource from lead where isnull= true]
for (Integer i = 0; i < 200; i++) {
Leads.add(new lead(
name='Dream force'+i
));
}
insert Leads;
}
I am not sure what is wrong here
Create an Apex class that implements the Schedulable interface to update Lead records with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class. This is very similar to what you did for Batch Apex.
Create an Apex class called 'DailyLeadProcessor' that uses the Schedulable interface.
The execute method must find the first 200 Leads with a blank LeadSource field and update them with the LeadSource value of 'Dreamforce'.
Create an Apex test class called 'DailyLeadProcessorTest'.
In the test class, insert 200 Lead records, schedule the DailyLeadProcessor class to run and test that all Lead records were updated correctly.
The unit tests must cover all lines of code included in the DailyLeadProcessor class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
Here is my code so far
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
list<leads>Lead = [select leadSource from lead where isnull= true]
for (Integer i = 0; i < 200; i++) {
Leads.add(new lead(
name='Dream force'+i
));
}
insert Leads;
}
I am not sure what is wrong here
Regards,
Mahesh
All Answers
Did you write the Test Class as well. If not please write a Test Class for your scheduler class.
Regards,
Mahesh
It says
"The execute method must find the first 200 Leads with a blank LeadSource field and update them with the LeadSource value of 'Dreamforce'."
means you have to query first 200 Lead records where LeadSource is null and update them in your scheduler, but you are creating the Leads.
Regards,
Mahesh.
Regards,
Mahesh
For the test class do I have to repeat anything from here to test? (I am new to this)
In the Test Class, you have to insert 200 Lead records with empty LeadSource and try to call the scheduler.
Regards,
Mahesh
Please add the code using the button "Add a Code Sample (< >)" in the above panel so that it will be pasted properly.
Regards,
Mahesh
Test Class looks good but if possible try to use the below code for cron exp:
Regards,
Mahesh
Regards,
Mahesh
Please take the below code:
Regards,
Mahesh
Please find the below code:
Schedule Class:
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Lead> lList = [Select Id, LeadSource from Lead where LeadSource = null limit 200];
list<lead> led = new list<lead>();
if(!lList.isEmpty()) {
for(Lead l: lList) {
l.LeadSource = 'Dreamforce';
led.add(l);
}
update led;
}
}
}
Test Class:
@isTest
public class DailyLeadProcessorTest{
static testMethod void testMethod1()
{
Test.startTest();
List<Lead> lstLead = new List<Lead>();
for(Integer i=0 ;i <200;i++)
{
Lead led = new Lead();
led.FirstName ='FirstName';
led.LastName ='LastName'+i;
led.Company ='demo'+i;
lstLead.add(led);
}
insert lstLead;
DailyLeadProcessor ab = new DailyLeadProcessor();
String jobId = System.schedule('jobName','0 5 * * * ? ' ,ab) ;
Test.stopTest();
}
}
Please find above code which will test if the leads are updated.
global class DailyLeadProcessor implements schedulable
{
global void execute(SchedulableContext ctx)
{
list<lead> leadlst=[select id,lastname,company,status,LeadSource from lead where LeadSource =null limit 200];
for(integer i=0;i<leadlst.size();i++)
{
leadlst[i].leadsource= 'Dreamforce';
}
update leadlst;
system.debug('leadlst '+leadlst.size() );
}
}
=============================================
Test class: took refrence from above solution as well.
@istest
private class DailyLeadProcessorTest {
public static string cron_exp='0 0 1 * * ?';
static testmethod void testScheduledJob()
{
Test.startTest();
List<Lead> leadlst = new list<lead>() ;
integer counta=0;
for(integer i=0;i<200;i++)
{
leadlst.add(new lead(lastname='singh',company='Company'+i,status= 'Open'));
counta = counta+1;
}
insert leadlst;
system.debug(counta);
String jobId = System.schedule('DailyLeadProcessor',CRON_EXP,new DailyLeadProcessor());
Test.stopTest();
}
}
DailyLeadProcessor Apex class:
public class DailyLeadProcessor implements Schedulable {
Public void execute(SchedulableContext SC){
List<Lead> LeadObj=[SELECT Id from Lead where LeadSource=null limit 200];
for(Lead l:LeadObj){
l.LeadSource='Dreamforce';
update l;
}
}
}
DailyLeadProcessorTest TEST CLASS:
@isTest
private class DailyLeadProcessorTest {
public static String CRON_EXP = '0 0 0 DATE MONTH ? YEAR';
static TestMethod void tetslead(){
List<Lead> l= new List<Lead>();
lead l1= new Lead();
l1.LastName='surya';
l1.Company='Company';
l1.Status='Closed-Converted';
l.add(l1);
insert l;
Test.startTest();
String jobId = System.schedule('ScheduledApexTest',CRON_EXP,new DailyLeadProcessor());
Test.stopTest();
}
}
I tried the code mentioned by you, but getting following errors:
DailyLeadProcessorTest: Invalid constructor syntax, name=value pairs can only be used for SObjects: Lead
DailyLeadProcessorTest: DML requires SObject or SObject list type: List<Lead>
DailyLeadProcessorTest: Invalid initializer type List<Lead> found for Map<Id,Lead>: expected a Map with the same key and value types, or a valid SObject List
DailyLeadProcessorTest: Illegal assignment from List<Lead> to List<Lead>
DailyLeadProcessorTest: Illegal assignment from List<Lead> to List<Lead>
DailyLeadProcessor: Illegal assignment from List<Lead> to List<Lead>
DailyLeadProcessor: Variable does not exist: LeadSource
DailyLeadProcessor: DML requires SObject or SObject list type: List<Lead>
Kindly assist with the resolution.
Regards
Chetan
Please refer below Apex Class and TestClass.
1.DailyLeadProcessor
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Lead> lList = [Select Id, LeadSource from Lead where LeadSource = null];
if(!lList.isEmpty()) {
for(Lead l: lList) {
l.LeadSource = 'Dreamforce';
}
update lList;
}
}
}
2.DailyLeadProcessorTest
@isTest
public class DailyLeadProcessorTest {
//Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
public static String CRON_EXP = '0 0 0 2 6 ? 2022';
static testmethod void testScheduledJob(){
List<Lead> leads = new List<Lead>();
for(Integer i = 0; i < 200; i++){
Lead lead = new Lead(LastName = 'Test ' + i, LeadSource = '', Company = 'Test Company ' + i, Status = 'Open - Not Contacted');
leads.add(lead);
}
insert leads;
Test.startTest();
// Schedule the test job
String jobId = System.schedule('Update LeadSource to DreamForce', CRON_EXP, new DailyLeadProcessor());
// Stopping the test will run the job synchronously
Test.stopTest();
}
}
3.Code Coverage:-
Test Class can have assert also :
@isTest
private class DailyLeadProcessorTest {
public static String CRON_EXP = '0 0 0 2 6 ? 2022';
@isTest static void test_method_one() {
List<Lead> lList = new List<Lead>();
for(Integer i =0;i <200 ;i++){
Lead lObj = new Lead(LastName = 'Test'+i, LeadSource =null,Company = 'Test Company'+i, Status ='Open - Not Contacted');
lList.add(lObj);
}
if(lList.size() >0){
Database.insert(lList,false);
}
Test.startTest();
// Schedule the test job
String jobId = System.schedule('Update LeadSource to DreamForce', CRON_EXP, new DailyLeadProcessor());
// Stopping the test will run the job synchronously
Test.stopTest();
System.assertEquals(200 , [Select count() From Lead where LeadSource ='Dreamforce' ]);
}
}
global void execute(SchedulableContext ctxt){
List<Lead> updatedLeads = new List<Lead>();
List<Lead> leads = [SELECT Name, LeadSource, Id FROM Lead WHERE LeadSource = '' LIMIT 200];
for(Lead lead : leads){
lead.LeadSource = 'Dreamforce';
updatedLeads.add(lead);
}
update updatedLeads;
}
}
Test class:
@isTest
public class DailyLeadProcessorTest {
public static String CRON_EXP = '0 0 0 23 10 ? 2018';
static testMethod void testScheduleJob(){
List<Lead> leads = new List<Lead>();
for(Integer i =0; i<200; i++){
leads.add(new Lead(lastName = 'TestScheduleApex ' + i,
company = 'TrailHead Asyn Apex',
leadsource = ''));
}
insert leads;
//execute batch
Test.startTest();
String jobid = System.schedule('Apex Scheduleable Handson', CRON_EXP, new DailyLeadProcessor());
Test.stopTest();
System.assertEquals(200, [SELECT count() from Lead WHERE leadsource = 'Dreamforce']);
}
}
@isTest
private class DailyLeadProcessorTest {
@testSetup
static void setup()
{
List<Lead> lstOfLead = new List<Lead>();
for(Integer i = 1; i<= 200; i++)
{
Lead ld = new Lead(Company = 'Comp' +i,LastName = 'LN' + i, Status = 'Working - Contacted');
lstofLead.add(ld);
}
Insert lstOfLead;
}
static testmethod void testDailyLeadProcessorScheduledJob() {
String sch = '0 5 12 * * ?';
Test.startTest();
String jobId = System.schedule('ScheduleApexText', sch, new DailyLeadProcessor());
}
}
DailyLeadProcessor class
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Lead> lList = [Select Id, LeadSource from Lead where LeadSource = null limit 200];
list<lead> led = new list<lead>();
if(!lList.isEmpty()) {
for(Lead l: lList) {
l.LeadSource = 'Dreamforce';
led.add(l);
}
update led;
}
}
}
DailyLeadProcessorTest class
@isTest
public class DailyLeadProcessorTest{
static testMethod void testMethod1()
{
Test.startTest();
List<Lead> lstLead = new List<Lead>();
for(Integer i=0 ;i <200;i++)
{
Lead led = new Lead();
led.FirstName ='FirstName';
led.LastName ='LastName'+i;
led.Company ='demo'+i;
lstLead.add(led);
}
insert lstLead;
DailyLeadProcessor ab = new DailyLeadProcessor();
String jobId = System.schedule('jobName','0 5 * * * ? ' ,ab) ;
Test.stopTest();
}
}
I am getting above mentioned error messages upon trying the code. Kindly advise.
VISHAL SARASWAT Thanks worked.....
public class DailyLeadProcessor implements schedulable
{
public void execute(SchedulableContext sct)
{
List<Lead> leadList = [Select id,lastname,company,status,leadsource from Lead where Leadsource = null LIMIT 200];
List<Lead> insertLeadList = new List<Lead>();
for(Lead led:leadList)
{
led.LeadSource = 'DreamForce';
insertLeadList.add(led);
}
if(insertLeadList.size() > 0)
{
update insertLeadList;
}
}
}
DailyLeadProcessorTest class
@isTest
public class DailyLeadProcessorTest
{
public static String CRON_EXP = '25 2 0 8 10 ?';
@testSetup
public static void setup()
{
List<Lead> leadList = new List<Lead>();
for(Integer i=0;i<200;i++)
{
leadList.add(new Lead(lastname='paul'+i,company='p.Tech'+i,state='working'));
}
insert leadList;
}
@isTest
public static void test()
{
Test.startTest();
Id jobId = System.schedule('Daily Lead Processor',CRON_EXP,new DailyLeadProcessor());
Test.stopTest();
System.assertEquals(200,[select count() from Lead where Leadsource = 'DreamForce']);
}
}
I tried above mentioned code, but got the error message.
Kindly advise.
@isTest
public class DailyLeadProcessorTest {
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
static testMethod void testDailyLeadProcessorTest() {
List<Lead> listLead = new List<Lead>();
for (Integer i=0; i<200; i++) {
Lead ll = new Lead();
ll.LastName = 'Test' + i;
ll.Company = 'Company' + i;
ll.Status = 'Open - Not Contacted';
listLead.add(ll);
}
insert listLead;
Test.startTest();
DailyLeadProcessor daily = new DailyLeadProcessor();
String jobId = System.schedule('Update LeadSource to Dreamforce', CRON_EXP, daily);
List<Lead> liss = new List<Lead>([SELECT Id, LeadSource FROM Lead WHERE LeadSource != 'Dreamforce']);
Test.stopTest();
}
}
*******************************
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
//Retrieving the 200 first leads where lead source is in blank.
List<Lead> leads = [SELECT ID, LeadSource FROM Lead where LeadSource = '' LIMIT 200];
//Setting the LeadSource field the 'Dreamforce' value.
for (Lead lead : leads) {
lead.LeadSource = 'Dreamforce';
}
//Updating all elements in the list.
update leads;
}
}
********************
@isTest
public class DailyLeadProcessorTest {
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
static testMethod void testDailyLeadProcessorTest() {
List<Lead> listLead = new List<Lead>();
for (Integer i=0; i<200; i++) {
Lead ll = new Lead();
ll.LastName = 'Test' + i;
ll.Company = 'Company' + i;
ll.Status = 'Open - Not Contacted';
listLead.add(ll);
}
insert listLead;
Test.startTest();
DailyLeadProcessor daily = new DailyLeadProcessor();
String jobId = System.schedule('Update LeadSource to Dreamforce', CRON_EXP, daily);
List<Lead> liss = new List<Lead>([SELECT Id, LeadSource FROM Lead WHERE LeadSource != 'Dreamforce']);
Test.stopTest();
}
}
I tried the above mentioned code, and got some error message with both of them.
PFB the attached error messages for your reference
global class DailyLeadProcessor implements Schedulable{
global void execute(SchedulableContext context) {
List<Lead> leadList = [SELECT Id, Name FROM Lead WHERE LeadSource='' LIMIT 200];
List<Lead> leadListForUpdate = new List<Lead>();
for(Lead ld : leadList){
ld.LeadSource = 'Dreamforce';
leadListForUpdate.add(ld);
}UPDATE leadListForUpdate;
}
}
Apex Test Class with Assert
@isTest
private class DailyLeadProcessorTest {
public static String CRON_EXP = '0 0 0 15 3 ? 2022';//Midnight on 15 march
public static List<Lead> leadList = new List<Lead>();
@testSetup
private static void testSetup() {
for(Integer i=0;i<200;i++){
leadList.add(new Lead(LastName='Test '+i,LeadSource='',Company='Briskminds'));
}INSERT leadList;
}
private static testMethod void testData() {
//All asynchronous processes w'll wun synchronously after test.stopTest();
test.startTest();
//Schedule the test jobs
String jobId = System.Schedule('ScheduledApexTest', CRON_EXP, new DailyLeadProcessor());
//Verify that the scheduled job has't run yet
Integer newSizeIs = [SELECT count() FROM Lead WHERE LeadSource='Dreamforce'];
Integer oldSizeIs = [SELECT count() FROM Lead WHERE LeadSource=''];
System.assertEquals(200, oldSizeIs);
System.assertEquals(0, newSizeIs);
System.assertEquals(0, leadList.size());
test.stopTest();
//Validate that the job runs, and the records were updated successfully with the LeadSource='Dreamforce'
System.assertEquals(200, [SELECT count() FROM Lead WHERE LeadSource='Dreamforce'], 'Records were updated successfully');
System.assertEquals(0, [SELECT count() FROM Lead WHERE LeadSource='']);
}
}
i got this error i don't know ...
please take the below code
Mark as the best answer if it sloves your ploblem
Thanks,
Sowmya
global void execute(SchedulableContext ctx){
List<Lead> leads = [SELECT Id, LeadSource FROM Lead WHERE LeadSource = ''];
if(leads.size() > 0){
List<Lead> newLeads = new List<Lead>();
for(Lead lead : leads){
lead.LeadSource = 'DreamForce';
newLeads.add(lead);
}
update newLeads;
}
}
}
private class DailyLeadProcessorTest{
//Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
public static String CRON_EXP = '0 0 0 2 7 ? 2022';
static testmethod void testScheduledJob(){
List<Lead> leads = new List<Lead>();
for(Integer i = 0; i < 200; i++){
Lead lead = new Lead(LastName = 'Test ' + i, LeadSource = '', Company = 'Test Company ' + i, Status = 'Open - Not Contacted');
leads.add(lead);
}
insert leads;
Test.startTest();
// Schedule the test job
String jobId = System.schedule('Update LeadSource to DreamForce', CRON_EXP, new DailyLeadProcessor());
// Stopping the test will run the job synchronously
Test.stopTest();
}
}
"System.AsyncException: Based on configured schedule, the given trigger 'SCHEDULED_APEX_JOB_TYPE.000000000000000' will never fire."