+ Start a Discussion
Swap s 9Swap s 9 

Create an Apex class that uses Scheduled Apex to update Lead records (Trail Head Challenge)

Hi,
Below is the Schedule Class with Test Class:

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();
    }
}
Ajay K DubediAjay K Dubedi
Hi,
Try this :
global class 	DailyLeadProcessor  implements    Database.Batchable<Sobject> 
{
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        return Database.getQueryLocator([Select LeadSource From Lead LIMIT 200 ]);
    }

    global void execute(Database.BatchableContext bc, List<Lead> scope)
    {
            for (Lead Leads : scope) 
            {
                Leads.LeadSource = 'Dreamforce';
            }
        update scope;
    }    

    global void finish(Database.BatchableContext bc){   }    
}
Test class :
@isTest 
public class 	DailyLeadProcessor Test 
{
    static testMethod void testMethod1() 
    {
        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;
        
        Test.startTest();

            	DailyLeadProcessor  obj = new 	DailyLeadProcessor ();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Regards,
Ajay
Sindhu1234Sindhu1234
You have missed the Start and finish methods in you apex class.
Ajay mishraAjay mishra
Hi,

Try below code in class it will be executed.
  1. public class DailyLeadProcessor implements Schedulable 
  2. {
  3.     public void execute(SchedulableContext sc) 
  4.     {
  5.         List<Lead> lList = [Select Id, LeadSource from Lead where LeadSource = null limit 200];
  6.         List<lead> led = new list<lead>();
  7.         if(!lList.isEmpty()) 
  8.         {
  9.             for(Lead l: lList) 
  10.             {
  11.                 l.LeadSource = 'Dreamforce';
  12.                 led.add(l);
  13.             }
  14.             Database.update(led);
  15.         }
  16.     }
  17. }