Test Class for Lead Conversion with Converted Account, Contact and Opportunity Ids

Test Class for Lead Conversion with Converted Account, Contact and Opportunity Ids

Sample Trigger:

trigger LeadTrigger on Lead ( after update ) {

    Set < Id > setAccountIds = new Set < Id >();
    Set < Id > setContactIds = new Set < Id >();
    Set < Id > setOpptyIds = new Set < Id >();
   
    for ( Lead objLead : trigger.new ) {
       
        /* Getting Account Ids from the Lead after conversion .
           ConvertedAccountId in Lead contains the Account Id to which the Lead is converted.
        */
        if ( objLead.IsConverted && objLead.IsConverted != trigger.oldMap.get( objLead.Id ).IsConverted ) {
            
            if ( String.isNotBlank( objLead.ConvertedAccountId ) ){
            
                setAccountIds.add( objLead.ConvertedAccountId );
            
            }
            
            if ( String.isNotBlank( objLead.ConvertedContactId ) ){
            
                setContactIds.add( objLead.ConvertedContactId );
            
            }
            
            if ( String.isNotBlank( objLead.ConvertedOpportunityId ) ){
            
                setOpptyIds.add( objLead.ConvertedOpportunityId );
            
            }
            
        }
   
    }
   
    if ( setAccountIds.size() > 0 ) {
   
        System.debug( 'Inside Account Block' );
   
    }
   
    if ( setContactIds.size() > 0 ) {
   
        System.debug( 'Inside Contact Block' );
   
    }
   
    if ( setOpptyIds.size() > 0 ) {
   
        System.debug( 'Inside Opportunity Block' );
   
    }

}

Sample Test Class:

@isTest  
private class LeadTriggerTest {  
	  
	static testMethod void testLeadConvert() {  
		  
		Lead objLead = new Lead( FirstName = 'Test', LastName = 'Sample', Company = 'Testing Sample Co' );  
		insert objLead;  
		  
		Database.LeadConvert lc = new database.LeadConvert();  
		lc.setLeadId( objLead.Id );  
		lc.setConvertedStatus( 'Closed - Converted' );  
		  
		Database.LeadConvertResult lcr = Database.convertLead( lc, false );  
		  
		System.debug( 'Errors are ' + lcr.getErrors() );  
		System.assert( lcr.isSuccess() );  
		  
	}  
  
} 

Leave a Reply