Advertisement
Learnify_Rectify

Use Batch Apex

Jun 22nd, 2024
8,911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | Source Code | 0 0
  1. Use Batch Apex from (module - Asynchronous Apex)
  2.  
  3. -------------------------------------------------
  4. SOURCE CODE1: LeadProcessor
  5.  
  6. public class LeadProcessor implements Database.Batchable<sObject> {
  7. public Database.QueryLocator start(Database.BatchableContext bc) {
  8. return Database.getQueryLocator([Select LeadSource From Lead ]);
  9. }
  10. public void execute(Database.BatchableContext bc, List<Lead> leads){
  11. for (Lead Lead : leads) {
  12. lead.LeadSource = 'Dreamforce';
  13. }
  14. update leads;
  15. }
  16. public void finish(Database.BatchableContext bc){
  17. } }
  18.  
  19. -------------------------------------------------
  20. SOURCE CODE2: LeadProcessorTest
  21.  
  22. @isTest
  23. public class LeadProcessorTest {
  24. @testSetup
  25. static void setup() {
  26. List<Lead> leads = new List<Lead>();
  27. for(Integer counter=0 ;counter <200;counter++){
  28. Lead lead = new Lead();
  29. lead.FirstName ='FirstName';
  30. lead.LastName ='LastName'+counter;
  31. lead.Company ='demo'+counter;
  32. leads.add(lead);
  33. }
  34. insert leads;
  35. }
  36. @isTest static void test() {
  37. Test.startTest();
  38. LeadProcessor leadProcessor = new LeadProcessor();
  39. Id batchId = Database.executeBatch(leadProcessor);
  40. Test.stopTest();   
  41.     } }
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement