Solving Use Batch Apex Challenge



The Trailhead challenge says:



Create an Apex class that uses Batch Apex to update Lead records.


Create an Apex class that implements the Database.Batchable interface to update all Lead records in the org with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class.

  • 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.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.


So first you must to create the LeadProcessor class:


global class LeadProcessor implements Database.Batchable<sObject>, Database.Stateful {

    //Creating a variable that will keep the count of Leads processed:
    global Integer recordsProcessed = 0;

    //Retrieving all Leads records (First step in Batch)
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT ID, LeadSource FROM Lead]);
    }

    //Processing all retrieved records (Second step in Batch)
    global void execute(Database.BatchableContext bc, List<Lead> scope) {
        for (Lead lead : scope) {
            lead.LeadSource = 'Dreamforce';
            recordsProcessed = recordsProcessed + 1;
            System.debug(lead.LeadSource);
        }
        update scope;
    }

    //Finishing (Final step in Batch)
    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
    }
}



And then you can create the LeadProcessorTest class:




@isTest
private class LeadProcessorTest {
    
    //Creating 200 lead records to Test
    @TestSetup
    static void setup(){
        List<Lead> leads = new List<Lead>();

        for (Integer i = 0; i < 200; i++) {
            //Adding a new lead to the lead list 
            leads.add(new Lead(LastName='Lead ' + i, Company='Company Number ' + i, Status='Open - Not Contacted'));
        }

        //Inserting the lead list
        insert leads;
    }

    static testMethod void test() {  

        Test.startTest();
        LeadProcessor lp = new LeadProcessor();
        Id batchId = Database.executeBatch(lp);
        Test.stopTest();        

        // after the testing stops, assert records were updated properly
        System.assertEquals(200, [select count() from lead where LeadSource = 'Dreamforce']);

    }
}





Comentarios

Publicar un comentario

Entradas más populares de este blog

Solving Subscribe to a Platform Event in an Apex Trigger Challange

Solving Schedule Jobs Using the Apex Scheduler