Skip to content

Instantly share code, notes, and snippets.

@mannharleen
Created November 1, 2016 04:12
Salesforce trailhead - Asynchronous Apex Using Batch Apex: The code creates a batch class to update lead object
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();
}
}
@mannharleen
Copy link
Author

Important note: A test class cannot pass more than 200 records to a batchclass to process. This is the reason we have limited i<200 in the code above

@sumedhdeshpande123
Copy link

how to get 100% code coverage?
can we write test case for execute method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment