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
Newbie999Newbie999 

can someone please tell me why I am not getting 100% code coverage?

below is my code
@isTest
private class DailyLeadProcessorTest {
    public static string t='0 0 12 7 17 ?';
    static testmethod void testscheduleLead(){
        /*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;*/
        list<lead> newleads= new list<lead>();
    for(integer i=0;i<200;i++){
        
       lead l= new lead(LastName='test lastName'+i,company='test company');
        newleads.add(l);
    
    }
        insert newleads;
        test.startTest();
        
         Integer n=[select count() from Lead where LastName like '%test%'];
        system.assertEquals(200,n,'200 new leads not created');
        integer c=[select count() from lead where leadSource='Dreamforce'];
        system.assertEquals(0,c, 'dreamforce leads found');
            //DailyLeadProcessor d= new DailyLeadProcessor();
            string jobId= system.schedule('scheduleleadprocessor',t,new DailyLeadProcessor());
            test.stopTest();
    

    }}
 
global class DailyLeadProcessor implements schedulable {
    global void execute(SchedulableContext ctx){
        list<lead> list1=[select id, name, leadSource from lead where leadSource=null];
        if (!list1.isEmpty()){
        for(lead l: list1){
            l.leadSource='Dreamforce';
            
        }
            update list1;}
            
    }

}
Best Answer chosen by Newbie999
Deepali KulshresthaDeepali Kulshrestha
Hi Bhavana,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved with 100% code coverage)

-------------- Schedule Class------- -----
global class DailyLeadProcessor implements schedulable {
    global void execute(SchedulableContext ctx){
        list<lead> list1=[select id, name, leadSource from lead where leadSource=null];
        if (!list1.isEmpty()){
            for(lead l: list1){
                l.leadSource='Dreamforce';

            }
            update list1;}

    }

}

----------------Test Class-------------------

@isTest
private class DailyLeadProcessorTest {
    public static string t='0 0 12 7 11 ?';
    static testmethod void testscheduleLead(){
        /*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;*/
        list<lead> newleads= new list<lead>();
        for(integer i=0;i<200;i++){

            lead l= new lead(LastName='test lastName'+i,company='test company');
            newleads.add(l);

        }
        insert newleads;
        test.startTest();

        Integer n=[select count() from Lead where LastName like '%test%'];
        system.assertEquals(200,n,'200 new leads not created');
        integer c=[select count() from lead where leadSource='Dreamforce'];
        system.assertEquals(0,c, 'dreamforce leads found');
        //DailyLeadProcessor d= new DailyLeadProcessor();
        String Jobid=system.schedule('scheduleleadprocessor',t,new DailyLeadProcessor());
        test.stopTest();
    }
}

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.

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Bhavana,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved with 100% code coverage)

-------------- Schedule Class------- -----
global class DailyLeadProcessor implements schedulable {
    global void execute(SchedulableContext ctx){
        list<lead> list1=[select id, name, leadSource from lead where leadSource=null];
        if (!list1.isEmpty()){
            for(lead l: list1){
                l.leadSource='Dreamforce';

            }
            update list1;}

    }

}

----------------Test Class-------------------

@isTest
private class DailyLeadProcessorTest {
    public static string t='0 0 12 7 11 ?';
    static testmethod void testscheduleLead(){
        /*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;*/
        list<lead> newleads= new list<lead>();
        for(integer i=0;i<200;i++){

            lead l= new lead(LastName='test lastName'+i,company='test company');
            newleads.add(l);

        }
        insert newleads;
        test.startTest();

        Integer n=[select count() from Lead where LastName like '%test%'];
        system.assertEquals(200,n,'200 new leads not created');
        integer c=[select count() from lead where leadSource='Dreamforce'];
        system.assertEquals(0,c, 'dreamforce leads found');
        //DailyLeadProcessor d= new DailyLeadProcessor();
        String Jobid=system.schedule('scheduleleadprocessor',t,new DailyLeadProcessor());
        test.stopTest();
    }
}

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.
This was selected as the best answer
Newbie999Newbie999
Thanks Deepali. It is still giving '0/1 test method passed error' in my org. But I tried it in different playground and it worked fine :)