Created
November 1, 2016 04:12
-
-
Save mannharleen/5b2a731f43d62ba8a8fa6b1abdf5f86c to your computer and use it in GitHub Desktop.
Salesforce trailhead - Asynchronous Apex Using Batch Apex: The code creates a batch class to update lead object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to get 100% code coverage?
can we write test case for execute method?