+ Start a Discussion
farah sheriffarah sherif 

scheduler trailhead module help

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.
Best Answer chosen by farah sherif
Raj VakatiRaj Vakati
Use this code
global class DailyLeadProcessor implements Schedulable{  
   global void execute(SchedulableContext sc){  
     List<Lead> lstOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];  
     List<Lead> lstOfUpdatedLead = new List<Lead>();  
     if(!lstOfLead.isEmpty()){  
       for(Lead ld : lstOfLead){  
         ld.LeadSource = 'Dreamforce';  
         lstOfUpdatedLead.add(ld);  
       }  
       UPDATE lstOfUpdatedLead;  
     }      
   }  
 }

test class
@isTest  
 private class DailyLeadProcessorTest{  
   @testSetup  
   static void setup(){  
     List<Lead> listOfLead = new List<Lead>();  
     for(Integer i = 1; i <= 200; i++){  
       Lead ld = new Lead(Company = 'Comp' + i ,LastName = 'LN'+i, Status = 'Working - Contacted');  
       listOfLead.add(ld);  
     }  
     Insert listOfLead;  
   }  
   static testmethod void testDailyLeadProcessorScheduledJob(){  
     String sch = '0 5 12 * * ?';  
     Test.startTest();  
     String jobId = System.schedule('ScheduledApexTest', sch, new DailyLeadProcessor());  
     List<Lead> listOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];  
     System.assertEquals(200, listOfLead.size());  
     Test.stopTest();  
   }  
 }

 

All Answers

Raj VakatiRaj Vakati
Use this code
global class DailyLeadProcessor implements Schedulable{  
   global void execute(SchedulableContext sc){  
     List<Lead> lstOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];  
     List<Lead> lstOfUpdatedLead = new List<Lead>();  
     if(!lstOfLead.isEmpty()){  
       for(Lead ld : lstOfLead){  
         ld.LeadSource = 'Dreamforce';  
         lstOfUpdatedLead.add(ld);  
       }  
       UPDATE lstOfUpdatedLead;  
     }      
   }  
 }

test class
@isTest  
 private class DailyLeadProcessorTest{  
   @testSetup  
   static void setup(){  
     List<Lead> listOfLead = new List<Lead>();  
     for(Integer i = 1; i <= 200; i++){  
       Lead ld = new Lead(Company = 'Comp' + i ,LastName = 'LN'+i, Status = 'Working - Contacted');  
       listOfLead.add(ld);  
     }  
     Insert listOfLead;  
   }  
   static testmethod void testDailyLeadProcessorScheduledJob(){  
     String sch = '0 5 12 * * ?';  
     Test.startTest();  
     String jobId = System.schedule('ScheduledApexTest', sch, new DailyLeadProcessor());  
     List<Lead> listOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];  
     System.assertEquals(200, listOfLead.size());  
     Test.stopTest();  
   }  
 }

 
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Farah,

Please try this code:

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;
        }
    }
}

For more information refer to these links:
http://sfdccodepractices.blogspot.com/2017/07/create-apex-class-that-uses-scheduled.html
https://www.youtube.com/watch?v=k__rAW5Gg9U
https://developer.salesforce.com/forums/?id=906F0000000DAGEIA4

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
farah sheriffarah sherif
Raj I don't understand this please xplain 
  String sch = '0 5 12 * * ?'; 
Raj VakatiRaj Vakati


Its cron expression for scheduler jon when to run ..



The System.Schedule method takes three arguments: a name for the job, an expression used to represent the time and date the job is scheduled to run, and the name of the class. This expression has the following syntax:



Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year


Refer this link 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
farah sheriffarah sherif
can I have your email for further explanation