Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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.
5 answers
  1. Mar 22, 2019, 3:17 AM

    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();

    }

    }

     
Loading
0/9000