+ Start a Discussion
RAJEEV REDDY 3RAJEEV REDDY 3 

Hi I am new to salesforce.How to write test class for the below Program.

public class LeadHandlerClass {
 public static void createContactRoleType (Map<id,lead> newMapLead, Map<id,Lead> oldMapLead){
        
        Set<Id> setOppId = new Set<Id>();
        Lead oldLead; 
        Lead newLead;
        Map<Id,Lead> mapContLead = new Map<Id,Lead>();
        
        for(Id CRId : newMapLead.keySet()){
            oldLead = OldMapLead.get(CRId);
            newLead = newMapLead.get(CRId);
            if(newLead.IsConverted && !oldLead.IsConverted){
                setOppId.add(newLead.ConvertedOpportunityId);
                mapContLead.put(newLead.ConvertedContactId,newLead);
            }
        }
        
        List<OpportunityContactRole> lsOppCtRole = [select id,ContactId, role from OpportunityContactRole where OpportunityId in: setOppId limit 50000];
        Lead tmpLead ;
        
        for(OpportunityContactRole oppCtRole : lsOppCtRole){
            tmpLead = mapContLead.get(OppCtRole.ContactId);
            OppctRole.Role = tmpLead.NIL_Contact_Type__c;
        }


        System.debug('Ls OppCtRole is ' + lsOppCtRole);
        if(!lsOppCtRole.isEmpty())
        {  
            Database.SaveResult[] results = Database.update(lsOppCtRole, false);
            for (Database.SaveResult res : results) {
                if (res.isSuccess()) {
                    System.debug('Successfully updated: ' + res.getId());
                } 
                else {
                    // This condition will be executed for failed records
                    for(Database.Error objErr : res.getErrors()) {
                        System.debug('The following error has occurred.');
                        // Printing error message in Debug log
                        System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
                        System.debug('Update failed by the error:' + objErr.getFields());
                    }
                }
            }
        }
        
    }
 
Maharajan CMaharajan C
Hi Rajeev,

Try the below test class:

@isTest
private class LeadHandlerClassTest
{
 
Static testMethod void comvertLeadTest()
{
        lead newLead = new lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';      // Add the proper status based on your org.
        newLead.Email = 'Test@test.com';
        // Add if there is any other Lead mandatory fields are required to create lead.
        insert newLead;
 
        test.startTest();
 
               database.leadConvert lc = new database.leadConvert();
               lc.setLeadId(newLead.id);
 
               leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
               lc.setConvertedStatus(convertStatus.MasterLabel);
 
               Database.LeadConvertResult lcr = Database.convertLead(lc);
               
         test.stopTest(); 
 
}
}

Thanks,
Maharajan.C
RAJEEV REDDY 3RAJEEV REDDY 3
Thanks Maharajan. I will try your code. Can you please tell me what is this MasterLabel