+ Start a Discussion
farah sheriffarah sherif 

batch apex trialhead module help

 Create an Apex class called 'LeadProcessor' that uses the Database.Batchable interface.
Use a QueryLocator in the start method to collect all Lead records in the org.
The execute method must update all Lead records in the org with the LeadSource value of 'Dreamforce'.
Create an Apex test class called 'LeadProcessorTest'.
In the test class, insert 200 Lead records, execute the 'LeadProcessor' Batch class and test that all Lead records were updated correctly.
The unit tests must cover all lines of code included in the LeadProcessor class, resulting in 100% code coverage.
Best Answer chosen by farah sherif
GovindarajGovindaraj
Hi Farah,

Please refer below code,

Batch Class :
global class LeadProcessor implements Database.batchable<sObject> {
    global Database.queryLocator start(Database.batchableContext bc) {
        //Query all Lead records
        String query = 'SELECT LeadSource FROM Lead';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.batchableContext bc, list<sObject> scope) {
        list<Lead> lstLead  = new list<Lead>();
        for(sObject s : scope) {
            Lead l = (Lead) s;
            l.LeadSource = 'Dreamforce';
            lstLead.add(l);
        }
        Update lstLead;
    }
    
    global void finish(Database.batchableContext bc) {
        //Post logic
    }
}

Test Class:
@isTest
Public class LeadProcessorTest {
    @isTest
    static void test_method_one() {
    
        list<Lead> lstLead = new list<Lead>();
        for(integer i=0 ; i<200 ; i++) {
            Lead l = new Lead();
            l.LastName = 'testLead' + i;
            l.Company = 'testCompany';
            l.Status = 'Open - Not Contacted';
            lstLead.add(l);
        }
        insert lstLead;
       
        Test.startTest();
            LeadProcessor lp = new LeadProcessor();
            Database.executeBatch(lp);
        Test.stopTest();
    }
}

​​​Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Farah,

Please refer below code,

Batch Class :
global class LeadProcessor implements Database.batchable<sObject> {
    global Database.queryLocator start(Database.batchableContext bc) {
        //Query all Lead records
        String query = 'SELECT LeadSource FROM Lead';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.batchableContext bc, list<sObject> scope) {
        list<Lead> lstLead  = new list<Lead>();
        for(sObject s : scope) {
            Lead l = (Lead) s;
            l.LeadSource = 'Dreamforce';
            lstLead.add(l);
        }
        Update lstLead;
    }
    
    global void finish(Database.batchableContext bc) {
        //Post logic
    }
}

Test Class:
@isTest
Public class LeadProcessorTest {
    @isTest
    static void test_method_one() {
    
        list<Lead> lstLead = new list<Lead>();
        for(integer i=0 ; i<200 ; i++) {
            Lead l = new Lead();
            l.LastName = 'testLead' + i;
            l.Company = 'testCompany';
            l.Status = 'Open - Not Contacted';
            lstLead.add(l);
        }
        insert lstLead;
       
        Test.startTest();
            LeadProcessor lp = new LeadProcessor();
            Database.executeBatch(lp);
        Test.stopTest();
    }
}

​​​Thanks,
Govindaraj.S
This was selected as the best answer
Raj VakatiRaj Vakati
This is the correct code
 
global class LeadProcessor implements Database.Batchable<sObject> {
    global Integer count = 0;
    
    global Database.QueryLocator start (Database.BatchableContext bc) {
        return Database.getQueryLocator('Select Id, LeadSource from lead');
    }
    
    global void execute (Database.BatchableContext bc,List<Lead> l_lst) {
        List<lead> l_lst_new = new List<lead>();
        for(lead l : l_lst) {
            l.leadsource = 'Dreamforce';
            l_lst_new.add(l);
            count+=1;
        }
        update l_lst_new;
    }
	
    global void finish (Database.BatchableContext bc) {
        system.debug('count = '+count);
    }
}
 
@isTest
public class LeadProcessorTest {
    
    @isTest    
    public static void testit() {
        List<lead> l_lst = new List<lead>();
        for (Integer i = 0; i<200; i++) {
            Lead l = new lead();
            l.LastName = 'name'+i;
            l.company = 'company';
            l.Status =  'somestatus';
            l_lst.add(l);
        }
        insert l_lst;
        
        test.startTest();
        
        Leadprocessor lp = new Leadprocessor();
        Id batchId = Database.executeBatch(lp);
        Test.stopTest();
            
    }

}