function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sidney ChowSidney Chow 

Schedule Jobs Using the Apex Scheduler challenge

I was working the Schedule Jobs Using Apex Scheduler challenge.  While I was able to solve the challenge by keeping only the System.schedule line and removing the rest under test(), I am curious on why, with the code below, it's giving me "variable does not exist: leads" on the Map line.  Why isn't it recognizing the leads variable?  Thanks!

@isTest
private class DailyLeadProcessorTest {
    public static string CRON_D = '0 0 0 15 3 ? 2022';
    @testSetup
    static void setup() {
        List<Lead> leads = new List<Lead>();
        for (Integer i=0; i<200; i++) {
            leads.add(new Lead(lastname='Last'+i, firstname='First', company='Company'));
        }
        insert leads;
        System.debug('Done inserting leads ' + leads);
    }
    @isTest
    static void test() {
    Map<Id, Lead> leadMap = new Map<Id, Lead>(leads);
    List<Id> leadIds = new List<Id>(leadMap.keySet());
    Test.startTest();
        String jobId = System.schedule('ScheduleApex Test', CRON_D, new DailyLeadProcessor());
        List<Task> lt = [select Id from task where whatid in :leadids];
        System.assertequals(0, lt.size(), 'Tasks exist before job has run');
    Test.stopTest();
    
    lt = [select id from task where whatid in :leadids];
    system.assertequals(leadids.size(), lt.size(),'Tasks were not created');
    }
}
AbhishekAbhishek (Salesforce Developers) 
Hi,

Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.

Trailhead Help (https://trailhead.salesforce.com/en/help?support=home)can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.


Please close the thread by selected as Best Answer so that we can keep our community clean
​​​​​​​

Regards,
Salesforce Support.
Sidney ChowSidney Chow
ok. I did not need this to pass the challenge, I already found another way to pass the challenge, so let me rephrase the question. Given the following code, why am I getting "variable does not exist: leads" on the Map leadMap = new Map(leads); line?